Assign (Update)
This operator is used to update node values. It can be used in either the:
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.
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.
c
clobber custom tags
Running
yq --null-input '.a.b = "cat" | .x = "frog"'
will output
a:
b: cat
x: frog
Given a sample.yml file of:
a:
b:
g: foof
then
yq '.a |= .b' sample.yml
will output
a:
g: foof
Given a sample.yml file of:
- 1
- 2
- 3
then
yq '.[] |= . * 2' sample.yml
will output
- 2
- 4
- 6
Note this will also work when the second file is a scalar (string/number)
Given a sample.yml file of:
a: apples
And another sample another.yml file of:
b: bob
then
yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0)' sample.yml another.yml
will output
a:
b: bob
Given a sample.yml file of:
a:
b: child
b: sibling
then
yq '.a = .b' sample.yml
will output
a: sibling
b: sibling
Given a sample.yml file of:
a: fieldA
b: fieldB
c: fieldC
then
yq '(.a, .c) = "potato"' sample.yml
will output
a: potato
b: fieldB
c: potato
Given a sample.yml file of:
a:
b: apple
then
yq '.a.b = "frog"' sample.yml
will output
a:
b: frog
Note there is no difference between
=
and |=
when the RHS is a scalarGiven a sample.yml file of:
a:
b: apple
then
yq '.a.b |= "frog"' sample.yml
will output
a:
b: frog
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: cactus
then
yq '(.a[] | select(. == "apple")) = "frog"' sample.yml
will output
a:
b: frog
c: cactus
Given a sample.yml file of:
- candy
- apple
- sandy
then
yq '(.[] | select(. == "*andy")) = "bogs"' sample.yml
will output
- bogs
- apple
- bogs
Given a sample.yml file of:
{}
then
yq '.a.b |= "bogs"' sample.yml
will output
a:
b: bogs
Anchor will remain
Given a sample.yml file of:
a: &cool cat
then
yq '.a = "dog"' sample.yml
will output
a: &cool dog
Given a sample.yml file of:
{}
then
yq '.a.b.[0] |= "bogs"' sample.yml
will output
a:
b:
- bogs
Given a sample.yml file of:
a: !cat meow
b: !dog woof
then
yq '.a = .b' sample.yml
will output
a: !cat woof
b: !dog woof
Use the
c
option to clobber custom tagsGiven a sample.yml file of:
a: !cat meow
b: !dog woof
then
yq '.a =c .b' sample.yml
will output
a: !dog woof
b: !dog woof
Last modified 6mo ago