yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Array subtraction
  • Array subtraction with nested array
  • Array subtraction with nested object
  • Number subtraction - float
  • Number subtraction - int
  • Decrement numbers
  • Date subtraction
  • Date subtraction - custom format
  • Custom types: that are really numbers

Was this helpful?

  1. Operators

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

- 1

Array subtraction with nested array

Running

yq --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 '. - [{"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 '.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 '.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 '.[] -= 1' sample.yml

will output

a: 2
b: 4

Date subtraction

Given a sample.yml file of:

a: 2021-01-01T03:10:00Z

then

yq '.a -= "3h10m"' sample.yml

will output

a: 2021-01-01T00:00:00Z

Date subtraction - custom format

Given a sample.yml file of:

a: Saturday, 15-Dec-01 at 6:00AM GMT

then

yq 'with_dtf("Monday, 02-Jan-06 at 3:04PM MST", .a -= "3h1m")' sample.yml

will output

a: Saturday, 15-Dec-01 at 2:59AM GMT

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:

a: !horse 2
b: !goat 1

then

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

will output

a: !horse 1
b: !goat 1
PreviousStyleNextTag

Last updated 2 years ago

Was this helpful?

You can subtract durations from dates. Assumes RFC3339 date time format, see for more information.

Use with_dtf to specify your datetime format. See for more information.

date-time operators
date-time operators