As a general rule, you should wrap paths in quotes to prevent your CLI from processing
 and other special characters.
a:b:c: thing # MATCHES
a:b:- c: thing0- c: thing1 # MATCHES- c: thing2
(e.g. when using the write command)
a:b:- c: thing0
Will add a new entry:
a:b:- c: thing0- c: thing1 # NEW entry from [+] on B array.
Negative array indexes can be used to traverse the array in reverse
Will access the last element in the
b array and yield:
a:b1:c: thing # MATCHESd: whateverb2:c: thing # MATCHESf: something irrelevant
bob:item:cats: bananas # MATCHESsomething:cats: lemonsitemThing:cats: more bananas # MATCHESitem2:cats: apples # MATCHESthing:cats: oranges
a:b:- c: thing0 # MATCHESd: what..ever- c: thing1 # MATCHESd: blarh- c: thing2 # MATCHESf: thingamabob
** will match arbitrary nodes for both maps and arrays:
a:b1:c: thing1 # MATCHESd: cat catb2:c: thing2 # MATCHESd: dog dogb3:d:- f:c: thing3 # MATCHESd: beep- f:g:c: thing4 # MATCHESd: boop- d: mooo
You can search children by nodes - note that this will return the parent of the matching expression, in the example below the parent(s) will be the matching indices of the 'a' array. We can then navigate down to get 'b.c' of each matching indice.
a:- b:c: thing0d: leopardba: fast- b:c: thing1 # MATCHESd: catba: meowy- b:c: thing2d: caterpillarba: icky- b:c: thing3 # MATCHESd: catba: also meowy
a:- b:c: thing0d: leopardba: fast- b:c: thing1 # MATCHESd: catba: meowy- b:c: thing2 # MATCHESd: caterpillarba: icky- b:c: thing3 # MATCHESd: catba: also meowy
animals:- dog- cat # MATCHES- rat
this also works in maps, and with prefixes
animals:friendliest: cow # MATCHESfavourite: cat # MATCHESsmallest: rat
When your yaml field has special characters that overlap with
yq path expression characters, you will need to escape them in order for the command to work.
When specifying a key that has a dot use key lookup indicator.
yaml r sample.yaml 'b."foo.bar"'
yaml w sample.yaml 'b."foo.bar"' 9
Any valid yaml key can be specified as part of a key lookup.
Note that the path is in single quotes to avoid the double quotes being interpreted by your shell.
The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags, if they start if a dash.
yq n -j -- --key --value
Will result in