I have sample.yaml file that looks like the following:
a:
b:
- "x"
- "y"
- "z"
and I have another file called toadd.yaml that contains the following
- "first to add"
- "second to add"
I want to modify sample.yaml file so that it looks like:
a:
b:
- "x
- "y"
- "z"
- "first to add"
- "second to add"
Also, I dont want redundant naming! so if there is "x" already in toadd.yaml than I dont want it to be added two times in sample.yaml/a.b
Please note that I have tried the following:
while read line; do
yq '.a.b = ['$line']' sample.yaml
done <toadd.yaml
and I fell on:
Error: Bad expression, could not find matching `]`
CodePudding user response:
If the files are relatively smaller, you could just directly load the second file on to the first. See Merging two files together
yq '.a.b = load("toadd.yaml")' sample.yaml
Tested on mikefarah/yq version 4.25.1
To solve the redundancy requirement, do a unique
operation before forming the array again.
yq 'load("toadd.yaml") as $data | .a.b |= ( . $data | unique )' sample.yaml
which can be further simplified to just
yq '.a.b |= ( . load("toadd.yaml") | unique )' sample.yaml