y
y
yq
Search…
Add
Add behaves differently according to the type of the LHS:
  • arrays: concatenate
  • number scalars: arithmetic addition
  • string scalars: concatenate
  • maps: shallow merge (use the multiply operator (*) to deeply merge)
Use += as a relative append assign for things like increment. Note that .a += .x is equivalent to running .a = .a + .x.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Concatenate arrays

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

Concatenate to existing array

Note that the styling of a is kept.
Given a sample.yml file of:
1
a: [1,2]
2
b:
3
- 3
4
- 4
Copied!
then
1
yq '.a += .b' sample.yml
Copied!
will output
1
a: [1, 2, 3, 4]
2
b:
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 '.a + null' sample.yml
Copied!
will output
1
- 1
2
- 2
Copied!

Append to existing array

Note that the styling is copied from existing array elements
Given a sample.yml file of:
1
a: ['dog']
Copied!
then
1
yq '.a += "cat"' sample.yml
Copied!
will output
1
a: ['dog', 'cat']
Copied!

Add new object to array

Given a sample.yml file of:
1
a:
2
- dog: woof
Copied!
then
1
yq '.a + {"cat": "meow"}' sample.yml
Copied!
will output
1
- dog: woof
2
- cat: meow
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 '.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:
11
b:
12
- mouse
Copied!

String concatenation

Given a sample.yml file of:
1
a: cat
2
b: meow
Copied!
then
1
yq '.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 '.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 '.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 '.[] += 1' sample.yml
Copied!
will output
1
a: 4
2
b: 6
Copied!

Date addition

You can add durations to dates. Assumes RFC3339 date time format, see date-time operators for more information.
Given a sample.yml file of:
1
a: 2021-01-01T00:00:00Z
Copied!
then
1
yq '.a += "3h10m"' sample.yml
Copied!
will output
1
a: 2021-01-01T03:10:00Z
Copied!

Date addition - custom format

You can add durations to dates. See date-time operators for more information.
Given a sample.yml file of:
1
a: Saturday, 15-Dec-01 at 2:59AM GMT
Copied!
then
1
yq 'with_dtf("Monday, 02-Jan-06 at 3:04PM MST", .a += "3h1m")' sample.yml
Copied!
will output
1
a: Saturday, 15-Dec-01 at 6:00AM GMT
Copied!

Add to null

Adding to null simply returns the rhs
Running
1
yq --null-input 'null + "cat"'
Copied!
will output
1
cat
Copied!

Add maps to shallow merge

Adding objects together shallow merges them. Use * to deeply merge.
Given a sample.yml file of:
1
a:
2
thing:
3
name: Astuff
4
value: x
5
a1: cool
6
b:
7
thing:
8
name: Bstuff
9
legs: 3
10
b1: neat
Copied!
then
1
yq '.a += .b' sample.yml
Copied!
will output
1
a:
2
thing:
3
name: Bstuff
4
legs: 3
5
a1: cool
6
b1: neat
7
b:
8
thing:
9
name: Bstuff
10
legs: 3
11
b1: neat
Copied!

Custom types: that are really strings

When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
1
a: !horse cat
2
b: !goat _meow
Copied!
then
1
yq '.a += .b' sample.yml
Copied!
will output
1
a: !horse cat_meow
2
b: !goat _meow
Copied!

Custom types: that are really numbers

When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
1
a: !horse 1.2
2
b: !goat 2.3
Copied!
then
1
yq '.a += .b' sample.yml
Copied!
will output
1
a: !horse 3.5
2
b: !goat 2.3
Copied!