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
cclobber custom tags
Create yaml file
Running
yq --null-input '.a.b = "cat" | .x = "frog"'will output
a:
b: cat
x: frogUpdate node to be the child value
Given a sample.yml file of:
a:
b:
g: foofthen
yq '.a |= .b' sample.ymlwill output
a:
g: foofDouble elements in an array
Given a sample.yml file of:
- 1
- 2
- 3then
yq '.[] |= . * 2' sample.ymlwill output
- 2
- 4
- 6Update node from another file
Note this will also work when the second file is a scalar (string/number)
Given a sample.yml file of:
a: applesAnd another sample another.yml file of:
b: bobthen
yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0)' sample.yml another.ymlwill output
a:
b: bobUpdate node to be the sibling value
Given a sample.yml file of:
a:
b: child
b: siblingthen
yq '.a = .b' sample.ymlwill output
a: sibling
b: siblingUpdated multiple paths
Given a sample.yml file of:
a: fieldA
b: fieldB
c: fieldCthen
yq '(.a, .c) = "potato"' sample.ymlwill output
a: potato
b: fieldB
c: potatoUpdate string value
Given a sample.yml file of:
a:
b: applethen
yq '.a.b = "frog"' sample.ymlwill output
a:
b: frogUpdate string value via |=
Note there is no difference between = and |= when the RHS is a scalar
Given a sample.yml file of:
a:
b: applethen
yq '.a.b |= "frog"' sample.ymlwill output
a:
b: frogUpdate 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:
a:
b: apple
c: cactusthen
yq '(.a[] | select(. == "apple")) = "frog"' sample.ymlwill output
a:
b: frog
c: cactusUpdate array values
Given a sample.yml file of:
- candy
- apple
- sandythen
yq '(.[] | select(. == "*andy")) = "bogs"' sample.ymlwill output
- bogs
- apple
- bogsUpdate empty object
Given a sample.yml file of:
{}then
yq '.a.b |= "bogs"' sample.ymlwill output
a:
b: bogsUpdate node value that has an anchor
Anchor will remain
Given a sample.yml file of:
a: &cool catthen
yq '.a = "dog"' sample.ymlwill output
a: &cool dogUpdate empty object and array
Given a sample.yml file of:
{}then
yq '.a.b.[0] |= "bogs"' sample.ymlwill output
a:
b:
- bogsCustom types are maintained by default
Given a sample.yml file of:
a: !cat meow
b: !dog woofthen
yq '.a = .b' sample.ymlwill output
a: !cat woof
b: !dog woofCustom types: clobber
Use the c option to clobber custom tags
Given a sample.yml file of:
a: !cat meow
b: !dog woofthen
yq '.a =c .b' sample.ymlwill output
a: !dog woof
b: !dog woofLast updated
Was this helpful?