Assign (Update)

This operator is used to update node values. It can be used in either the:

plain form: =

Which will set the LHS node values equal to the RHS node values. The RHS expression is run against the matching nodes in the pipeline.

relative form: |=

This will do a similar thing to the plain form, but the RHS expression is run with each LHS node as context. This is useful for updating values based on old values, e.g. increment.

Flags

  • c clobber custom tags

Create yaml file

Running

yq --null-input '.a.b = "cat" | .x = "frog"'

will output

a:
  b: cat
x: frog

Update node to be the child value

Given a sample.yml file of:

then

will output

Double elements in an array

Given a sample.yml file of:

then

will output

Update node from another file

Note this will also work when the second file is a scalar (string/number)

Given a sample.yml file of:

And another sample another.yml file of:

then

will output

Update node to be the sibling value

Given a sample.yml file of:

then

will output

Updated multiple paths

Given a sample.yml file of:

then

will output

Update string value

Given a sample.yml file of:

then

will output

Update string value via |=

Note there is no difference between = and |= when the RHS is a scalar

Given a sample.yml file of:

then

will output

Update deeply selected results

Note that the LHS is wrapped in brackets! This is to ensure we don't first filter out the yaml and then update the snippet.

Given a sample.yml file of:

then

will output

Update array values

Given a sample.yml file of:

then

will output

Update empty object

Given a sample.yml file of:

then

will output

Update node value that has an anchor

Anchor will remain

Given a sample.yml file of:

then

will output

Update empty object and array

Given a sample.yml file of:

then

will output

Custom types are maintained by default

Given a sample.yml file of:

then

will output

Custom types: clobber

Use the c option to clobber custom tags

Given a sample.yml file of:

then

will output

Last updated

Was this helpful?