2021-10-30 03:14:39 +00:00
# Assign (Update)
2021-11-03 04:00:28 +00:00
This operator is used to update node values. It can be used in either the:
2021-10-30 03:14:39 +00:00
2021-11-03 04:00:28 +00:00
### plain form: `=`
2023-03-18 03:54:23 +00:00
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.
2021-10-30 03:14:39 +00:00
2021-11-03 04:00:28 +00:00
### relative form: `|=`
2023-03-18 03:54:23 +00:00
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.
2022-08-29 08:31:50 +00:00
### Flags
- `c` clobber custom tags
2021-11-03 04:00:28 +00:00
## Create yaml file
Running
2021-10-30 03:14:39 +00:00
```bash
2022-01-28 01:45:43 +00:00
yq --null-input '.a.b = "cat" | .x = "frog"'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
b: cat
x: frog
```
## Update node to be the child value
Given a sample.yml file of:
```yaml
a:
b:
g: foof
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a |= .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
g: foof
```
## Double elements in an array
Given a sample.yml file of:
```yaml
- 1
- 2
- 3
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.[] |= . * 2' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
- 2
- 4
- 6
```
## 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:
```yaml
a: apples
```
And another sample another.yml file of:
```yaml
b: bob
```
then
```bash
yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0)' sample.yml another.yml
```
will output
```yaml
a:
b: bob
```
## Update node to be the sibling value
Given a sample.yml file of:
```yaml
a:
b: child
b: sibling
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a = .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: sibling
b: sibling
```
## Updated multiple paths
Given a sample.yml file of:
```yaml
a: fieldA
b: fieldB
c: fieldC
```
then
```bash
2022-05-25 01:01:35 +00:00
yq '(.a, .c) = "potato"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
2022-05-25 01:01:35 +00:00
a: potato
2021-10-30 03:14:39 +00:00
b: fieldB
2022-05-25 01:01:35 +00:00
c: potato
2021-10-30 03:14:39 +00:00
```
## Update string value
Given a sample.yml file of:
```yaml
a:
b: apple
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a.b = "frog"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
b: frog
```
## Update string value via |=
Note there is no difference between `=` and `|=` when the RHS is a scalar
Given a sample.yml file of:
```yaml
a:
b: apple
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a.b |= "frog"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
b: frog
```
2021-11-03 04:00:28 +00:00
## Update deeply selected results
2022-05-25 01:01:35 +00:00
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.
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a:
b: apple
c: cactus
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '(.a[] | select(. == "apple")) = "frog"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
b: frog
c: cactus
```
## Update array values
Given a sample.yml file of:
```yaml
- candy
- apple
- sandy
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '(.[] | select(. == "*andy")) = "bogs"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
- bogs
- apple
- bogs
```
## Update empty object
Given a sample.yml file of:
```yaml
{}
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a.b |= "bogs"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
2022-04-14 22:52:46 +00:00
a:
b: bogs
2021-10-30 03:14:39 +00:00
```
2021-12-04 03:08:16 +00:00
## Update node value that has an anchor
2023-03-18 03:54:23 +00:00
Anchor will remain
2021-12-04 03:08:16 +00:00
Given a sample.yml file of:
```yaml
a: & cool cat
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a = "dog"' sample.yml
2021-12-04 03:08:16 +00:00
```
will output
```yaml
a: & cool dog
```
2021-10-30 03:14:39 +00:00
## Update empty object and array
Given a sample.yml file of:
```yaml
{}
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a.b.[0] |= "bogs"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
2022-04-14 22:52:46 +00:00
a:
b:
- bogs
2021-10-30 03:14:39 +00:00
```
2021-11-03 04:00:28 +00:00
2022-08-29 08:31:50 +00:00
## Custom types are maintained by default
Given a sample.yml file of:
```yaml
a: !cat meow
b: !dog woof
```
then
```bash
yq '.a = .b' sample.yml
```
will output
```yaml
a: !cat woof
b: !dog woof
```
2023-03-18 03:54:23 +00:00
## Custom types: clobber
2022-08-29 08:31:50 +00:00
Use the `c` option to clobber custom tags
Given a sample.yml file of:
```yaml
a: !cat meow
b: !dog woof
```
then
```bash
yq '.a =c .b' sample.yml
```
will output
```yaml
a: !dog woof
b: !dog woof
```