yq/pkg/yqlib/doc/Update Assign Operator.md

123 lines
1.5 KiB
Markdown
Raw Normal View History

2020-11-17 22:44:16 +00:00
Updates the LHS using the expression on the RHS. Note that the RHS runs against the _original_ LHS value, so that you can evaluate a new value based on the old (e.g. increment).
## Examples
### Update parent to be the child value
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
```
### 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
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
```