Skip to content

Write/Update

yq w <yaml_file> <path> <new value>

To Stdout

Given a sample.yaml file of:

b:
  c: 2

then

yq w sample.yaml b.c cat

will output:

b:
  c: cat

From STDIN

cat sample.yaml | yq w - b.c blah

Adding new fields

Any missing fields in the path will be created on the fly.

Given a sample.yaml file of:

b:
  c: 2

then

yq w sample.yaml b.d[0] "new thing"

will output:

b:
  c: cat
  d:
    - new thing

Appending value to an array field

Given a sample.yaml file of:

b:
  c: 2
  d:
    - new thing
    - foo thing

then

yq w sample.yaml "b.d[+]" "bar thing"

will output:

b:
  c: cat
  d:
    - new thing
    - foo thing
    - bar thing

Note that the path is in quotes to avoid the square brackets being interpreted by your shell.

Multiple Documents - update a single document

Given a sample.yaml file of:

something: else
---
b:
  c: 2

then

yq w -d1 sample.yaml b.c 5

will output:

something: else
---
b:
  c: 5

Multiple Documents - update all documents

Given a sample.yaml file of:

something: else
---
b:
  c: 2

then

yq w -d'*' sample.yaml b.c 5

will output:

something: else
b:
  c: 5
---
b:
  c: 5

Note that '*' is in quotes to avoid being interpreted by your shell.

Updating files in-place

Given a sample.yaml file of:

b:
  c: 2

then

yq w -i sample.yaml b.c cat

will update the sample.yaml file so that the value of 'c' is cat.

Updating multiple values with a script

Given a sample.yaml file of:

b:
  c: 2
  e:
    - name: Billy Bob

and a script update_instructions.yaml of:

b.c: 3
b.e[0].name: Howdy Partner

then

yq w -s update_instructions.yaml sample.yaml

will output:

b:
  c: 3
  e:
    - name: Howdy Partner

And, of course, you can pipe the instructions in using '-':

cat update_instructions.yaml | yq w -s - sample.yaml

Values starting with a hyphen (or dash)

The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:

yq w -- my.path -3

will output

my:
  path: -3

Keys with dots

When specifying a key that has a dot use key lookup indicator.

b:
  foo.bar: 7
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 quotes to avoid the square brackets being interpreted by your shell.

Keys (and values) with leading dashes

If a key or value has leading dashes, yq won't know that you are passing a value as opposed to a flag (and you will get a 'bad flag syntax' error).

To fix that, you will need to tell it to stop processing flags by adding '--' after the last flag like so:

yq n -t -- --key --value

Will result in

` --key: --value