yq/pkg/yqlib/doc/operators/subtract.md
2021-12-21 15:02:07 +11:00

1.5 KiB

Subtract

You can use subtract to subtract numbers, as well as removing elements from an array.

Array subtraction

Running

yq eval --null-input '[1,2] - [2,3]'

will output

- 1

Array subtraction with nested array

Running

yq eval --null-input '[[1], 1, 2] - [[1], 3]'

will output

- 1
- 2

Array subtraction with nested object

Note that order of the keys does not matter

Given a sample.yml file of:

- a: b
  c: d
- a: b

then

yq eval '. - [{"c": "d", "a": "b"}]' sample.yml

will output

- a: b

Number subtraction - 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.5

then

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

will output

a: -1.5
b: 4.5

Number subtraction - 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.5

then

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

will output

a: -1.5
b: 4.5

Number subtraction - 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: 4

then

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

will output

a: -1
b: 4

Decrement numbers

Given a sample.yml file of:

a: 3
b: 5

then

yq eval '.[] -= 1' sample.yml

will output

a: 2
b: 4