y
y
yq
Search…
Subtract
You can use subtract to subtract numbers, as well as removing elements from an array.

Array subtraction

Running
1
yq eval --null-input '[1,2] - [2,3]'
Copied!
will output
1
- 1
Copied!

Array subtraction with nested array

Running
1
yq eval --null-input '[[1], 1, 2] - [[1], 3]'
Copied!
will output
1
- 1
2
- 2
Copied!

Array subtraction with nested object

Note that order of the keys does not matter
Given a sample.yml file of:
1
- a: b
2
c: d
3
- a: b
Copied!
then
1
yq eval '. - [{"c": "d", "a": "b"}]' sample.yml
Copied!
will output
1
- a: b
Copied!

Number subtraction - 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.5
Copied!
then
1
yq eval '.a = .a - .b' sample.yml
Copied!
will output
1
a: -1.5
2
b: 4.5
Copied!

Number subtraction - 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.5
Copied!
then
1
yq eval '.a = .a - .b' sample.yml
Copied!
will output
1
a: -1.5
2
b: 4.5
Copied!

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

Decrement 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: 2
2
b: 4
Copied!
Last modified 2mo ago