y
y
yq
Search…
Assign (Update)
This operator is used to update node values. It can be used in either the:

plain form: =

Which will assign the LHS node values 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, however, the RHS expression is run against the LHS nodes. This is useful for updating values based on old values, e.g. increment.

Create yaml file

Running
1
yq eval --null-input '.a.b = "cat" | .x = "frog"'
Copied!
will output
1
a:
2
b: cat
3
x: frog
Copied!

Update node to be the child value

Given a sample.yml file of:
1
a:
2
b:
3
g: foof
Copied!
then
1
yq eval '.a |= .b' sample.yml
Copied!
will output
1
a:
2
g: foof
Copied!

Double elements in an array

Given a sample.yml file of:
1
- 1
2
- 2
3
- 3
Copied!
then
1
yq eval '.[] |= . * 2' sample.yml
Copied!
will output
1
- 2
2
- 4
3
- 6
Copied!

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:
1
a: apples
Copied!
And another sample another.yml file of:
1
b: bob
Copied!
then
1
yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0)' sample.yml another.yml
Copied!
will output
1
a:
2
b: bob
Copied!

Update node to be the sibling value

Given a sample.yml file of:
1
a:
2
b: child
3
b: sibling
Copied!
then
1
yq eval '.a = .b' sample.yml
Copied!
will output
1
a: sibling
2
b: sibling
Copied!

Updated multiple paths

Given a sample.yml file of:
1
a: fieldA
2
b: fieldB
3
c: fieldC
Copied!
then
1
yq eval '(.a, .c) = "potatoe"' sample.yml
Copied!
will output
1
a: potatoe
2
b: fieldB
3
c: potatoe
Copied!

Update string value

Given a sample.yml file of:
1
a:
2
b: apple
Copied!
then
1
yq eval '.a.b = "frog"' sample.yml
Copied!
will output
1
a:
2
b: frog
Copied!

Update string value via |=

Note there is no difference between = and |= when the RHS is a scalar
Given a sample.yml file of:
1
a:
2
b: apple
Copied!
then
1
yq eval '.a.b |= "frog"' sample.yml
Copied!
will output
1
a:
2
b: frog
Copied!

Update deeply selected results

Note that the LHS is wrapped in brackets! This is to ensure we dont first filter out the yaml and then update the snippet.
Given a sample.yml file of:
1
a:
2
b: apple
3
c: cactus
Copied!
then
1
yq eval '(.a[] | select(. == "apple")) = "frog"' sample.yml
Copied!
will output
1
a:
2
b: frog
3
c: cactus
Copied!

Update array values

Given a sample.yml file of:
1
- candy
2
- apple
3
- sandy
Copied!
then
1
yq eval '(.[] | select(. == "*andy")) = "bogs"' sample.yml
Copied!
will output
1
- bogs
2
- apple
3
- bogs
Copied!

Update empty object

Given a sample.yml file of:
1
{}
Copied!
then
1
yq eval '.a.b |= "bogs"' sample.yml
Copied!
will output
1
{a: {b: bogs}}
Copied!

Update node value that has an anchor

Anchor will remaple
Given a sample.yml file of:
1
a: &cool cat
Copied!
then
1
yq eval '.a = "dog"' sample.yml
Copied!
will output
1
a: &cool dog
Copied!

Update empty object and array

Given a sample.yml file of:
1
{}
Copied!
then
1
yq eval '.a.b.[0] |= "bogs"' sample.yml
Copied!
will output
1
{a: {b: [bogs]}}
Copied!
Last modified 1mo ago