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.
Concatenate arrays
Given a sample.yml file of:
a:
- 1
- 2
b:
- 3
- 4then
yq '.a + .b' sample.ymlwill output
- 1
- 2
- 3
- 4Concatenate to existing array
Note that the styling of a is kept.
Given a sample.yml file of:
a: [1,2]
b:
- 3
- 4then
yq '.a += .b' sample.ymlwill output
a: [1, 2, 3, 4]
b:
- 3
- 4Concatenate null to array
Given a sample.yml file of:
a:
- 1
- 2then
yq '.a + null' sample.ymlwill output
- 1
- 2Append to existing array
Note that the styling is copied from existing array elements
Given a sample.yml file of:
a: ['dog']then
yq '.a += "cat"' sample.ymlwill output
a: ['dog', 'cat']Prepend to existing array
Given a sample.yml file of:
a:
- dogthen
yq '.a = ["cat"] + .a' sample.ymlwill output
a:
- cat
- dogAdd new object to array
Given a sample.yml file of:
a:
- dog: woofthen
yq '.a + {"cat": "meow"}' sample.ymlwill output
- dog: woof
- cat: meowRelative append
Given a sample.yml file of:
a:
a1:
b:
- cat
a2:
b:
- dog
a3: {}then
yq '.a[].b += ["mouse"]' sample.ymlwill output
a:
a1:
b:
- cat
- mouse
a2:
b:
- dog
- mouse
a3:
b:
- mouseString concatenation
Given a sample.yml file of:
a: cat
b: meowthen
yq '.a += .b' sample.ymlwill output
a: catmeow
b: meowNumber addition - float
If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
a: 3
b: 4.9then
yq '.a = .a + .b' sample.ymlwill output
a: 7.9
b: 4.9Number addition - int
If both the lhs and rhs are ints then the expression will be calculated with ints.
Given a sample.yml file of:
a: 3
b: 4then
yq '.a = .a + .b' sample.ymlwill output
a: 7
b: 4Increment numbers
Given a sample.yml file of:
a: 3
b: 5then
yq '.[] += 1' sample.ymlwill output
a: 4
b: 6Date 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:
a: 2021-01-01T00:00:00Zthen
yq '.a += "3h10m"' sample.ymlwill output
a: 2021-01-01T03:10:00ZDate addition - custom format
You can add durations to dates. See date-time operators for more information.
Given a sample.yml file of:
a: Saturday, 15-Dec-01 at 2:59AM GMTthen
yq 'with_dtf("Monday, 02-Jan-06 at 3:04PM MST", .a += "3h1m")' sample.ymlwill output
a: Saturday, 15-Dec-01 at 6:00AM GMTAdd to null
Adding to null simply returns the rhs
Running
yq --null-input 'null + "cat"'will output
catAdd maps to shallow merge
Adding objects together shallow merges them. Use * to deeply merge.
Given a sample.yml file of:
a:
thing:
name: Astuff
value: x
a1: cool
b:
thing:
name: Bstuff
legs: 3
b1: neatthen
yq '.a += .b' sample.ymlwill output
a:
thing:
name: Bstuff
legs: 3
a1: cool
b1: neat
b:
thing:
name: Bstuff
legs: 3
b1: neatCustom types: that are really strings
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
a: !horse cat
b: !goat _meowthen
yq '.a += .b' sample.ymlwill output
a: !horse cat_meow
b: !goat _meowCustom types: that are really numbers
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
a: !horse 1.2
b: !goat 2.3then
yq '.a += .b' sample.ymlwill output
a: !horse 3.5
b: !goat 2.3Last updated
Was this helpful?