2020-11-19 06:08:13 +00:00
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.
2020-11-17 22:44:16 +00:00
## Examples
2020-11-20 04:29:53 +00:00
### Update node to be the child value
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a:
b:
g: foof
```
then
```bash
yq eval '.a |= .b' sample.yml
```
will output
```yaml
2020-11-17 23:32:30 +00:00
a:
g: foof
```
2020-11-20 04:29:53 +00:00
### Update node to be the sibling value
2020-11-19 06:08:13 +00:00
Given a sample.yml file of:
```yaml
a:
b: child
b: sibling
```
then
```bash
yq eval '.a = .b' sample.yml
```
will output
```yaml
a: sibling
b: sibling
```
2020-11-17 23:32:30 +00:00
### Updated multiple paths
Given a sample.yml file of:
```yaml
a: fieldA
b: fieldB
c: fieldC
```
then
```bash
yq eval '(.a, .c) |= "potatoe"' sample.yml
```
will output
```yaml
a: potatoe
b: fieldB
c: potatoe
2020-11-17 22:44:16 +00:00
```
### Update string value
2020-11-19 06:08:13 +00:00
Given a sample.yml file of:
```yaml
a:
b: apple
```
then
```bash
yq eval '.a.b = "frog"' sample.yml
```
will output
```yaml
a:
b: frog
```
### Update string value via |=
Note there is no difference between `=` and `|=` when the RHS is a scalar
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a:
b: apple
```
then
```bash
yq eval '.a.b |= "frog"' sample.yml
```
will output
```yaml
2020-11-17 23:32:30 +00:00
a:
b: frog
2020-11-17 22:44:16 +00:00
```
### Update selected results
Given a sample.yml file of:
```yaml
a:
b: apple
c: cactus
```
then
```bash
yq eval '.a[] | select(. == "apple") |= "frog"' sample.yml
```
will output
```yaml
2020-11-17 23:32:30 +00:00
a:
b: frog
c: cactus
2020-11-17 22:44:16 +00:00
```
### Update array values
Given a sample.yml file of:
```yaml
- candy
- apple
- sandy
```
then
```bash
yq eval '.[] | select(. == "*andy") |= "bogs"' sample.yml
```
will output
```yaml
2020-11-17 23:32:30 +00:00
- bogs
- apple
- bogs
2020-11-17 22:44:16 +00:00
```
### Update empty object
Given a sample.yml file of:
```yaml
'': null
```
then
```bash
yq eval '.a.b |= "bogs"' sample.yml
```
will output
```yaml
2020-11-17 23:32:30 +00:00
'': null
a:
b: bogs
2020-11-17 22:44:16 +00:00
```
### Update empty object and array
Given a sample.yml file of:
```yaml
'': null
```
then
```bash
yq eval '.a.b[0] |= "bogs"' sample.yml
```
will output
```yaml
2020-11-17 23:32:30 +00:00
'': null
a:
b:
- bogs
2020-11-17 22:44:16 +00:00
```