Search code examples
bashyamlyq

Add yaml's map to a document using yq


I want to add a map (which maps a string to another string, e.g. fff: bar) to the following

aaa: hello
bbb: world
ccc: foo

so I'm using

document='aaa: hello
bbb: world
ccc: foo'
yq '. = . + {fff: bar}' <<<"$document"

but I get Error: 1:10: invalid input text "fff: bar}".

If I add a map of numbers, it works

document='aaa: hello
bbb: world 
ccc: foo' 
yq '. = . + {111: 222}' <<<"$document"

outputs

aaa: hello
bbb: world
ccc: foo
111: 222

What am I missing?

Thanks, Luca


Solution

  • You'll need to add quotes around the fff key and value:

    document='aaa: hello
    bbb: world
    ccc: foo'
    yq '. += {"fff": "bar"}' <<<"$document"
    aaa: hello
    bbb: world
    ccc: foo
    fff: bar
    ➜  ~
    ➜  ~
    

    I've also changed . = . + to the more readable . +=