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
  • Concatenate arrays
  • Concatenate to existing array
  • Concatenate null to array
  • Append to existing array
  • Prepend to existing array
  • Add new object to array
  • Relative append
  • String concatenation
  • Number addition - float
  • Number addition - int
  • Increment numbers
  • Date addition
  • Date addition - custom format
  • Add to null
  • Add maps to shallow merge
  • Custom types: that are really strings
  • Custom types: that are really numbers

Was this helpful?

  1. Operators

Add

Add behaves differently according to the type of the LHS:

  • arrays: concatenate

  • number scalars: arithmetic addition

  • string scalars: concatenate

  • maps: shallow merge (use the multiply operator (*) to deeply merge)

Use += as a relative append assign for things like increment. Note that .a += .x is equivalent to running .a = .a + .x.

Concatenate arrays

Given a sample.yml file of:

a:
  - 1
  - 2
b:
  - 3
  - 4

then

yq '.a + .b' sample.yml

will output

- 1
- 2
- 3
- 4

Concatenate to existing array

Note that the styling of a is kept.

Given a sample.yml file of:

a: [1,2]
b:
  - 3
  - 4

then

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

will output

a: [1, 2, 3, 4]
b:
  - 3
  - 4

Concatenate null to array

Given a sample.yml file of:

a:
  - 1
  - 2

then

yq '.a + null' sample.yml

will output

- 1
- 2

Append to existing array

Note that the styling is copied from existing array elements

Given a sample.yml file of:

a: ['dog']

then

yq '.a += "cat"' sample.yml

will output

a: ['dog', 'cat']

Prepend to existing array

Given a sample.yml file of:

a:
  - dog

then

yq '.a = ["cat"] + .a' sample.yml

will output

a:
  - cat
  - dog

Add new object to array

Given a sample.yml file of:

a:
  - dog: woof

then

yq '.a + {"cat": "meow"}' sample.yml

will output

- dog: woof
- cat: meow

Relative append

Given a sample.yml file of:

a:
  a1:
    b:
      - cat
  a2:
    b:
      - dog
  a3: {}

then

yq '.a[].b += ["mouse"]' sample.yml

will output

a:
  a1:
    b:
      - cat
      - mouse
  a2:
    b:
      - dog
      - mouse
  a3:
    b:
      - mouse

String concatenation

Given a sample.yml file of:

a: cat
b: meow

then

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

will output

a: catmeow
b: meow

Number addition - 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.9

then

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

will output

a: 7.9
b: 4.9

Number addition - 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: 7
b: 4

Increment numbers

Given a sample.yml file of:

a: 3
b: 5

then

yq '.[] += 1' sample.yml

will output

a: 4
b: 6

Date addition

Given a sample.yml file of:

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

then

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

will output

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

Date addition - custom format

Given a sample.yml file of:

a: Saturday, 15-Dec-01 at 2:59AM 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 6:00AM GMT

Add to null

Adding to null simply returns the rhs

Running

yq --null-input 'null + "cat"'

will output

cat

Add maps to shallow merge

Adding objects together shallow merges them. Use * to deeply merge.

Given a sample.yml file of:

a:
  thing:
    name: Astuff
    value: x
  a1: cool
b:
  thing:
    name: Bstuff
    legs: 3
  b1: neat

then

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

will output

a:
  thing:
    name: Bstuff
    legs: 3
  a1: cool
  b1: neat
b:
  thing:
    name: Bstuff
    legs: 3
  b1: neat

Custom types: that are really strings

When custom tags are encountered, yq will try to decode the underlying type.

Given a sample.yml file of:

a: !horse cat
b: !goat _meow

then

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

will output

a: !horse cat_meow
b: !goat _meow

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 1.2
b: !goat 2.3

then

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

will output

a: !horse 3.5
b: !goat 2.3
PreviousOperatorsNextAlternative (Default value)

Last updated 2 years ago

Was this helpful?

You can add durations to dates. Assumes RFC3339 date time format, see for more information.

You can add durations to dates. See for more information.

date-time operators
date-time operators