2020-11-19 06:08:13 +00:00
|
|
|
|
2020-12-01 03:06:49 +00:00
|
|
|
## Create yaml file
|
|
|
|
Running
|
|
|
|
```bash
|
2020-12-01 06:58:07 +00:00
|
|
|
yq eval --null-input '.a.b = "cat" | .x = "frog"'
|
2020-12-01 03:06:49 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b: cat
|
|
|
|
x: frog
|
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +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
|
|
|
|
```
|
|
|
|
|
2021-07-07 09:53:33 +00:00
|
|
|
## Double elements in an array
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- 3
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.[] |= . * 2' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- 2
|
|
|
|
- 4
|
|
|
|
- 6
|
|
|
|
```
|
|
|
|
|
2021-01-01 23:27:32 +00:00
|
|
|
## 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
|
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +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-22 02:16:54 +00:00
|
|
|
## Updated multiple paths
|
2020-11-17 23:32:30 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: fieldA
|
|
|
|
b: fieldB
|
|
|
|
c: fieldC
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2021-07-07 09:53:33 +00:00
|
|
|
yq eval '(.a, .c) = "potatoe"' sample.yml
|
2020-11-17 23:32:30 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: potatoe
|
|
|
|
b: fieldB
|
|
|
|
c: potatoe
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +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
|
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +00:00
|
|
|
## Update string value via |=
|
2020-11-19 06:08:13 +00:00
|
|
|
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
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +00:00
|
|
|
## Update selected results
|
2020-11-17 22:44:16 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b: apple
|
|
|
|
c: cactus
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2020-12-26 10:37:08 +00:00
|
|
|
yq eval '(.a[] | select(. == "apple")) = "frog"' sample.yml
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2020-11-17 23:32:30 +00:00
|
|
|
a:
|
|
|
|
b: frog
|
|
|
|
c: cactus
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +00:00
|
|
|
## Update array values
|
2020-11-17 22:44:16 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- candy
|
|
|
|
- apple
|
|
|
|
- sandy
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2020-12-01 06:58:07 +00:00
|
|
|
yq eval '(.[] | select(. == "*andy")) = "bogs"' sample.yml
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2020-11-17 23:32:30 +00:00
|
|
|
- bogs
|
|
|
|
- apple
|
|
|
|
- bogs
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +00:00
|
|
|
## Update empty object
|
2020-11-17 22:44:16 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2020-11-22 02:16:54 +00:00
|
|
|
{}
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a.b |= "bogs"' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2020-11-22 02:16:54 +00:00
|
|
|
{a: {b: bogs}}
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
|
2020-11-22 02:16:54 +00:00
|
|
|
## Update empty object and array
|
2020-11-17 22:44:16 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2020-11-22 02:16:54 +00:00
|
|
|
{}
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2020-11-28 00:24:16 +00:00
|
|
|
yq eval '.a.b.[0] |= "bogs"' sample.yml
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2020-11-22 02:16:54 +00:00
|
|
|
{a: {b: [bogs]}}
|
2020-11-17 22:44:16 +00:00
|
|
|
```
|
|
|
|
|