<p>I have <strong>sample.yaml</strong> file that looks like the following:</p>
<pre><code> a:
  b:
    - &quot;x&quot;
    - &quot;y&quot;
    - &quot;z&quot;
</code></pre>
<p>and I have another file called <strong>toadd.yaml</strong> that contains the following</p>
<pre><code>- &quot;first to add&quot;
- &quot;second to add&quot;
</code></pre>
<p>I want to modify <strong>sample.yaml</strong> file so that it looks like:</p>
<pre><code> a:
  b:
    - &quot;x
    - &quot;y&quot;
    - &quot;z&quot;
    - &quot;first to add&quot;
    - &quot;second to add&quot;
</code></pre>
<p>Also, I dont want redundant naming! so if there is &quot;x&quot; already in <strong>toadd.yaml</strong> than I dont want it to be added two times in <strong>sample.yaml/a.b</strong></p>
<p>Please note that I have tried the following:</p>
<pre><code>while read line; do
    yq '.a.b += ['$line']' sample.yaml
done &lt;toadd.yaml
</code></pre>
<p>and I fell on:</p>
<pre><code>Error: Bad expression, could not find matching `]`
</code></pre>

<p>If the files are relatively smaller, you could just directly load the second file on to the first. See <a href="https://mikefarah.gitbook.io/yq/operators/multiply-merge#merge-two-files-together" rel="nofollow noreferrer">Merging two files together</a></p>
<pre class="lang-none prettyprint-override"><code>yq '.a.b += load(&quot;toadd.yaml&quot;)' sample.yaml
</code></pre>
<p>Tested on <a href="https://github.com/mikefarah/yq/" rel="nofollow noreferrer">mikefarah/yq</a> version 4.25.1</p>
<p>To solve the redundancy requirement, do a <code>unique</code> operation before forming the array again.</p>
<pre class="lang-none prettyprint-override"><code>yq 'load(&quot;toadd.yaml&quot;) as $data | .a.b |= ( . + $data | unique )' sample.yaml
</code></pre>
<p>which can be further simplified to just</p>
<pre class="lang-none prettyprint-override"><code>yq '.a.b |= ( . + load(&quot;toadd.yaml&quot;) | unique )' sample.yaml
</code></pre>
