yq/pkg/yqlib/doc/Add.md
Mike Farah f7f8bed955 wip
2020-12-27 09:55:21 +11:00

1.4 KiB

Add behaves differently according to the type of the LHS:

  • arrays: concatenate
  • number scalars: arithmetic addition (soon)
  • string scalars: concatenate (soon)

Use += as append assign for things like increment. .a += .x is equivalent to running .a |= . + .x.

Concatenate and assign arrays

Given a sample.yml file of:

a: {val: thing, b: [cat, dog]}
'': null

then

yq eval '.a.b += ["cow"]' sample.yml

will output

a: {val: thing, b: [cat, dog, cow]}
'': null

Concatenate arrays

Given a sample.yml file of:

a: [1, 2]
b: [3, 4]
'': null

then

yq eval '.a + .b' sample.yml

will output

[1, 2, 3, 4]

Concatenate null to array

Given a sample.yml file of:

a: [1, 2]
'': null

then

yq eval '.a + null' sample.yml

will output

[1, 2]

Add object to array

Given a sample.yml file of:

a: [1, 2]
c: {cat: meow}
'': null

then

yq eval '.a + .c' sample.yml

will output

[1, 2, {cat: meow}]

Add string to array

Given a sample.yml file of:

a: [1, 2]
'': null

then

yq eval '.a + "hello"' sample.yml

will output

[1, 2, hello]

Update array (append)

Given a sample.yml file of:

a: [1, 2]
b: [3, 4]
'': null

then

yq eval '.a = .a + .b' sample.yml

will output

a: [1, 2, 3, 4]
b: [3, 4]
'': null