y
y
yq
Search…
Add
Add behaves differently according to the type of the LHS:
  • arrays: concatenate
  • number scalars: arithmetic addition
  • string scalars: concatenate
Use += as append assign for things like increment. Note that .a += .x is equivalent to running .a = .a + .x.

Concatenate and assign arrays

Given a sample.yml file of:
1
a:
2
val: thing
3
b:
4
- cat
5
- dog
Copied!
then
1
yq eval '.a.b += ["cow"]' sample.yml
Copied!
will output
1
a:
2
val: thing
3
b:
4
- cat
5
- dog
6
- cow
Copied!

Concatenate arrays

Given a sample.yml file of:
1
a:
2
- 1
3
- 2
4
b:
5
- 3
6
- 4
Copied!
then
1
yq eval '.a + .b' sample.yml
Copied!
will output
1
- 1
2
- 2
3
- 3
4
- 4
Copied!

Concatenate null to array

Given a sample.yml file of:
1
a:
2
- 1
3
- 2
Copied!
then
1
yq eval '.a + null' sample.yml
Copied!
will output
1
- 1
2
- 2
Copied!

Add new object to array

Given a sample.yml file of:
1
a:
2
- dog: woof
Copied!
then
1
yq eval '.a + {"cat": "meow"}' sample.yml
Copied!
will output
1
- dog: woof
2
- cat: meow
Copied!

Add string to array

Given a sample.yml file of:
1
a:
2
- 1
3
- 2
Copied!
then
1
yq eval '.a + "hello"' sample.yml
Copied!
will output
1
- 1
2
- 2
3
- hello
Copied!

Append to array

Given a sample.yml file of:
1
a:
2
- 1
3
- 2
4
b:
5
- 3
6
- 4
Copied!
then
1
yq eval '.a = .a + .b' sample.yml
Copied!
will output
1
a:
2
- 1
3
- 2
4
- 3
5
- 4
6
b:
7
- 3
8
- 4
Copied!

Append another array using +=

Given a sample.yml file of:
1
a:
2
- 1
3
- 2
4
b:
5
- 3
6
- 4
Copied!
then
1
yq eval '.a += .b' sample.yml
Copied!
will output
1
a:
2
- 1
3
- 2
4
- 3
5
- 4
6
b:
7
- 3
8
- 4
Copied!

Relative append

Given a sample.yml file of:
1
a:
2
a1:
3
b:
4
- cat
5
a2:
6
b:
7
- dog
8
a3: {}
Copied!
then
1
yq eval '.a[].b += ["mouse"]' sample.yml
Copied!
will output
1
a:
2
a1:
3
b:
4
- cat
5
- mouse
6
a2:
7
b:
8
- dog
9
- mouse
10
a3: {b: [mouse]}
Copied!

String concatenation

Given a sample.yml file of:
1
a: cat
2
b: meow
Copied!
then
1
yq eval '.a = .a + .b' sample.yml
Copied!
will output
1
a: catmeow
2
b: meow
Copied!

Number addition - float

If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
1
a: 3
2
b: 4.9
Copied!
then
1
yq eval '.a = .a + .b' sample.yml
Copied!
will output
1
a: 7.9
2
b: 4.9
Copied!

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:
1
a: 3
2
b: 4
Copied!
then
1
yq eval '.a = .a + .b' sample.yml
Copied!
will output
1
a: 7
2
b: 4
Copied!

Increment numbers

Given a sample.yml file of:
1
a: 3
2
b: 5
Copied!
then
1
yq eval '.[] += 1' sample.yml
Copied!
will output
1
a: 4
2
b: 6
Copied!

Add to null

Adding to null simply returns the rhs
Running
1
yq eval --null-input 'null + "cat"'
Copied!
will output
1
cat
Copied!
Last modified 2mo ago