Subtract
You can use subtract to subtract numbers as well as remove elements from an array.
Array subtraction
Running
yq --null-input '[1,2] - [2,3]'will output
- 1Array subtraction with nested array
Running
yq --null-input '[[1], 1, 2] - [[1], 3]'will output
- 1
- 2Array subtraction with nested object
Note that order of the keys does not matter
Given a sample.yml file of:
- a: b
c: d
- a: bthen
yq '. - [{"c": "d", "a": "b"}]' sample.ymlwill output
- a: bNumber 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.5then
yq '.a = .a - .b' sample.ymlwill output
a: -1.5
b: 4.5Number 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: 4then
yq '.a = .a - .b' sample.ymlwill output
a: -1
b: 4Decrement numbers
Given a sample.yml file of:
a: 3
b: 5then
yq '.[] -= 1' sample.ymlwill output
a: 2
b: 4Date 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:
a: 2021-01-01T03:10:00Zthen
yq '.a -= "3h10m"' sample.ymlwill output
a: 2021-01-01T00:00:00ZDate subtraction - custom format
Use with_dtf to specify your datetime format. See date-time operators for more information.
Given a sample.yml file of:
a: Saturday, 15-Dec-01 at 6:00AM GMTthen
yq 'with_dtf("Monday, 02-Jan-06 at 3:04PM MST", .a -= "3h1m")' sample.ymlwill output
a: Saturday, 15-Dec-01 at 2:59AM GMTCustom 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 2
b: !goat 1then
yq '.a -= .b' sample.ymlwill output
a: !horse 1
b: !goat 1Last updated
Was this helpful?