2020-11-24 02:07:19 +00:00
|
|
|
Add behaves differently according to the type of the LHS:
|
|
|
|
- arrays: concatenate
|
2021-01-11 04:43:50 +00:00
|
|
|
- number scalars: arithmetic addition
|
|
|
|
- string scalars: concatenate
|
2020-11-25 04:01:12 +00:00
|
|
|
|
2021-01-11 04:43:50 +00:00
|
|
|
Use `+=` as append assign for things like increment. Note that `.a += .x` is equivalent to running `.a = .a + .x`.
|
2020-11-28 00:24:16 +00:00
|
|
|
|
|
|
|
## Concatenate and assign arrays
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
val: thing
|
|
|
|
b:
|
|
|
|
- cat
|
|
|
|
- dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a.b += ["cow"]' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
val: thing
|
|
|
|
b:
|
|
|
|
- cat
|
|
|
|
- dog
|
|
|
|
- cow
|
|
|
|
```
|
|
|
|
|
2020-11-24 02:07:19 +00:00
|
|
|
## Concatenate arrays
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
b:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a + .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
```
|
|
|
|
|
|
|
|
## Concatenate null to array
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a + null' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
```
|
|
|
|
|
2021-01-11 04:52:06 +00:00
|
|
|
## Add new object to array
|
2020-11-24 02:07:19 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
2021-01-11 04:52:06 +00:00
|
|
|
- dog: woof
|
2020-11-24 02:07:19 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2021-01-11 04:52:06 +00:00
|
|
|
yq eval '.a + {"cat": "meow"}' sample.yml
|
2020-11-24 02:07:19 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2021-01-11 04:52:06 +00:00
|
|
|
- dog: woof
|
2020-11-24 02:07:19 +00:00
|
|
|
- cat: meow
|
|
|
|
```
|
|
|
|
|
|
|
|
## Add string to array
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a + "hello"' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- hello
|
|
|
|
```
|
|
|
|
|
|
|
|
## Update array (append)
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
b:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a = .a + .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
b:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
```
|
|
|
|
|
2021-01-11 04:43:50 +00:00
|
|
|
## String concatenation
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: meow
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a = .a + .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: catmeow
|
|
|
|
b: meow
|
|
|
|
```
|
|
|
|
|
|
|
|
## Relative string concatenation
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: meow
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a += .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: catmeow
|
|
|
|
b: meow
|
|
|
|
```
|
|
|
|
|
|
|
|
## Number addition - float
|
|
|
|
If the lhs or rhs are floats then the expression will be calculated with floats.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: 3
|
|
|
|
b: 4.9
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a = .a + .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: 7.9
|
|
|
|
b: 4.9
|
|
|
|
```
|
|
|
|
|
|
|
|
## Number addition - int
|
|
|
|
If both the lhs and rhs are ints then the expression will be calculated with ints.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: 3
|
|
|
|
b: 4
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a = .a + .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: 7
|
|
|
|
b: 4
|
|
|
|
```
|
|
|
|
|
|
|
|
## Increment number
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: 3
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a += 1' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: 4
|
|
|
|
```
|
|
|
|
|
2021-01-18 02:58:46 +00:00
|
|
|
## Add to null
|
|
|
|
Adding to null simply returns the rhs
|
|
|
|
|
|
|
|
Running
|
|
|
|
```bash
|
|
|
|
yq eval --null-input 'null + "cat"'
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cat
|
|
|
|
```
|
|
|
|
|