You can use the field [+] notation to append fields in yq v3. So your command results in

yq --style double w -i myfile.yaml 'spec.template.spec.containers[0].command[+]' "c"

The above command appends the entry "c" to the array.

Or if you want to update the whole array command you could do in v4. Note that this creates the array entries in separate lines than what is shown in the OP. See YAML Multi-Line Arrays

yq e '.spec.template.spec.containers[0].command |= ["a", "b", "c","d"] | ..style="double"' yaml

Note that yq v4 is already out there and supports much powerful path expressions and operations. See Upgrading from v3 for instructions

Answer from Inian on Stack Overflow
🌐
yq
mikefarah.gitbook.io › yq › operators › add
Add | yq - GitBook
January 14, 2023 - Note that .a += .x is equivalent to running .a = .a + .x. Given a sample.yml file of: Copy · a: - 1 - 2 b: - 3 - 4 · then · Copy · yq '.a + .b' sample.yml · will output · Note that the styling of a is kept. Given a sample.yml file of: then · will output · Given a sample.yml file of: then · will output · Note that the styling is copied from existing array elements ·
Discussions

[question][solved]: how do I add to an array contents from another file
src: _anchors_: - &_test 123 dst: _anchors_: - &_test 123 - $_myVar | multilineCertFromFileLine0 multilineCertFromFileLine1 multilineCertFromFileLine2 More on github.com
🌐 github.com
1
1
Syntax to write and append array with v4
Describe the bug Advice on v4 append to array syntax compared to v2. In version 2 appending to an array is nicely concise, the only deficiency is not handling an empty file. In version 4 it seems v... More on github.com
🌐 github.com
3
December 22, 2020
How to Add / Replace array elements with "yq" conditionally - Stack Overflow
I am looking for some help with using "yq" to replace / add existing array elements by name. Also conditionally? Appreciate your help in this regard. I have tried the below so far.. but t... More on stackoverflow.com
🌐 stackoverflow.com
yq syntax to append a modified array element to a YAML file - Stack Overflow
I want to select elements of set where x==a, manipulate them, and append the result to the original array. For example: set: - x: a y: aaa - x: b y: bbb - x: a y: ccc - x: A y: aaa - x: A y: ccc ... I just realized that my solution only worked for kislyuk/yq. yq -y '.set += (.set | map(select(.x == "a") | .x = "A"))' ... Sign up to request clarification or add ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › mikefarah › yq › issues › 613
Syntax to write and append array with v4 · Issue #613 · mikefarah/yq
December 22, 2020 - file=data1.yml yq eval --null-input '.key.array=[]' > $file for x in a b c; do yq eval ".
Author   hobti01
🌐
yq
mikefarah.gitbook.io › yq › operators › collect-into-array
Collect into Array | yq - GitBook
October 29, 2022 - This creates an array using the expression between the square brackets · Given a sample.yml file of:
🌐
GitHub
github.com › mikefarah › yq › discussions › 1544
How to insert an object into the list at the starting · mikefarah/yq · Discussion #1544
Have a look here: https://mikefarah.gitbook.io/yq/operators/add#append-to-existing-array, and directly underneath that is how to prepend :)
Author   mikefarah
Find elsewhere
🌐
GitHub
github.com › mikefarah › yq › issues › 1686
Add item to an nested array which uses an environment variable · Issue #1686 · mikefarah/yq
June 2, 2023 - I wanted to add an element to an array '.spec.template.spec.containers[0].args' the value to be added is constructed using an environment variable. ... server_name="main-local" yq -i '.spec.template.spec.containers[0].args +="--server-name=env(server_name)"' <file_path>
Author   unmarshall
🌐
GitHub
github.com › mikefarah › yq › issues › 405
Array items insertion in .yaml file · Issue #405 · mikefarah/yq
April 9, 2020 - again and again, i want to pass the array name like ${elementsArray} , so that it can insert all the records at once in .yaml file. like: yq w sample.yaml b.d[+] ${elementsArray}
Author   avinashGuptaIos
🌐
cheat.sh
cheat.sh › yq
cheat.sh/yq
For version 4, use 'yq_v4' # Read spec.template node from example.yml yq r example.yml spec.template # Read from stdin cat sample.yaml | yq r - b.c # Print the path yq r --printMode p "a.thing*.*" # Print the path and value yq r --printMode pv "a.thing*.*" # Print the length of a list yq r sample.yml --length animals # Read with conditions yq r sample.yml spec.metadata[name==myapp] # Collect results into an array yq r sample.yaml --collect a.*.animal # Read from the 2nd document yq r -d1 sample.yaml b.c # Validate a document
🌐
Fabian Lee
fabianlee.org › 2022 › 12 › 20 › yq-update-deeply-nested-elements-in-yaml
yq: update deeply nested elements in yaml | Fabian Lee : Software Engineer
March 11, 2023 - $ yq '(.. | select(has("tags")).tags) ... tags: - coreos - arm64 · Or if we wanted to append array elements, we could use the “+=” operator...
🌐
Baeldung
baeldung.com › home › files › file editing › processing yaml content with yq
Processing YAML Content With yq | Baeldung on Linux
March 18, 2024 - $ yq 'to_entries' personal_data.yaml - key: name value: "My name" - key: surname value: "Foo" - key: street_address value: city: "My city" street: "My street" number: 1 · The results are provided as array elements.
🌐
GitHub
github.com › mikefarah › yq › discussions › 2161
Add a record/array element and then insert yaml file as key of that `record` · mikefarah/yq · Discussion #2161
What I need to do is create a new "record" (i.e. array element) in y1.yaml from a bash heredoc (below) and then add y2.yaml under a new details: key of that new record/element saving it all back to y1.yaml.
Author   mikefarah
🌐
GitHub
github.com › mikefarah › yq › discussions › 1716
How to add an object to a list if it doesn't exist? · mikefarah/yq · Discussion #1716
July 13, 2023 - See https://mikefarah.gitbook.io/yq/operators/boolean-operators#all_c-returns-true-if-all-elements-in-the-array-are-true-for-the-given-condition; https://mikefarah.gitbook.io/yq/operators/select and https://mikefarah.gitbook.io/yq/operators/with · Beta Was this translation helpful?
Author   mikefarah
Top answer
1 of 1
2

The following is an expression for Mike Farah's yq that loads the name from cen1.yaml and puts it into the internal yq variable $name. It then creates a new YAML document and loads cen2.yaml into it, under the key given by $name:

load("cen1.yaml").name as $name | .[$name] = load("cen2.yaml")

We can use this to loop over several subdirectories, applying yq in each, and collecting the output in final.yaml. The code assumes that the directories all match the filename globbing pattern directory_*:

for dir in directory_*; do
(
    cd "$dir" &&
    yq -n 'load("cen1.yaml").name as $name | .[$name] = load("cen2.yaml")'
)
done >final.yaml

We use -n with yq since we don't use any files given on the command line.

Given the data in your question (corrected by removing the dotted lines and inserting spaces after each :), this would produce the following YAML file:

xxxx:
  service: ser1
  image:
    field1: field1
    field2: field2
another:
  tv: lcd
  name:
    field1: field1
    field2: field2

If all directories are not available at once, you can build your final.yaml file by appending bits to it:

rm -f final.yaml

# Get directory_1, and then...
(
    cd directory_1 &&
    yq -n 'load("cen1.yaml").name as $name | .[$name] = load("cen2.yaml")'
) >>final.yaml

# Get directory_2, and then...
(
    cd directory_2 &&
    yq -n 'load("cen1.yaml").name as $name | .[$name] = load("cen2.yaml")'
) >>final.yaml

# etc.

... essentially running each iteration of the loop manually and appending the output to the result file in each step.


The same thing, but with Andrey Kislyuk's yq (a wrapper around jq):

for dir in directory_*; do
(
    cd "$dir" &&
    yq -n -y 'input.name as $name | .[$name] = inputs' cen1.yaml cen2.yaml
)
done >final.yaml
🌐
yq
mikefarah.gitbook.io › yq › recipes
Recipes | yq - GitBook
See the assign and add operators for more information. Say we are only interested in child1 and child2, and want to filter everything else out. ... We have an array and we want to update the elements with a particular name in reference to its type.
🌐
Stack Overflow
stackoverflow.com › questions › tagged › yq
Highest scored 'yq' questions - Stack Overflow
I'm having a big problem with the edit in place flag for yq version 2.12.0. We are trying to update a value for a variable in one of our .yaml scripts. The before looks like this... authentication: ... ... I have a yaml array like below, identitymappings: - arn: "arn:aws:iam::12345567:role/AdmRole" group: "system:masters" user: "user1" - arn: "arn:aws:iam::12345567:...