y
y
yq
Search…
Subtract
You can use subtract to subtract numbers, as well as removing elements from an array.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Array subtraction

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

Array subtraction with nested array

Running
1
yq --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 '. - [{"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 '.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 '.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 '.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 '.[] -= 1' sample.yml
Copied!
will output
1
a: 2
2
b: 4
Copied!

Date subtraction

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

Date subtraction - custom format

Use with_dtf to specify your datetime format. See date-time operators for more information.
Given a sample.yml file of:
1
a: Saturday, 15-Dec-01 at 6:00AM 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 2:59AM GMT
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 2
2
b: !goat 1
Copied!
then
1
yq '.a -= .b' sample.yml
Copied!
will output
1
a: !horse 1
2
b: !goat 1
Copied!
Last modified 4mo ago