y
y
yq
Search…
y
y
yq
v4.x
yq
How It Works
Upgrading from V3
Commands
Evaluate
Evaluate All
Shell Completion
Operators
Add
Alternative (Default value)
Anchor and Alias Operators
Assign (Update)
Boolean Operators
Collect into Array
Column
Comment Operators
Compare Operators
Contains
Create, Collect into Object
Date Time
Delete
Document Index
Encode / Decode
Entries
Env Variable Operators
Equals
Eval
File Operators
Flatten
Group By
Has
Keys
Length
Line
Load
Map
Multiply (Merge)
Parent
Path
Pick
Pipe
Recursive Descent (Glob)
Reduce
Reverse
Select
Sort
Sort Keys
Split into Documents
String Operators
Style
Subtract
Tag
Traverse (Read)
Union
Unique
Variable Operators
With
Usage
Output format
Working with CSV, TSV
Working with JSON
Working with Properties
Working with XML
Front Matter
Split into multiple files
GitHub Action
Tips, Tricks, Troubleshooting
Github Page
Powered By
GitBook
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
.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>
Concatenate arrays
Given a sample.yml file of:
1
a
:
2
-
1
3
-
2
4
b
:
5
-
3
6
-
4
Copied!
then
1
yq
'.a + .b'
sample.yml
Copied!
will output
1
-
1
2
-
2
3
-
3
4
-
4
Copied!
Concatenate to existing array
Note that the styling of
a
is kept.
Given a sample.yml file of:
1
a
:
[
1
,
2
]
2
b
:
3
-
3
4
-
4
Copied!
then
1
yq
'.a += .b'
sample.yml
Copied!
will output
1
a
:
[
1
,
2
,
3
,
4
]
2
b
:
3
-
3
4
-
4
Copied!
Concatenate null to array
Given a sample.yml file of:
1
a
:
2
-
1
3
-
2
Copied!
then
1
yq
'.a + null'
sample.yml
Copied!
will output
1
-
1
2
-
2
Copied!
Append to existing array
Note that the styling is copied from existing array elements
Given a sample.yml file of:
1
a
:
[
'dog'
]
Copied!
then
1
yq
'.a += "cat"'
sample.yml
Copied!
will output
1
a
:
[
'dog'
,
'cat'
]
Copied!
Add new object to array
Given a sample.yml file of:
1
a
:
2
-
dog
:
woof
Copied!
then
1
yq
'.a + {"cat": "meow"}'
sample.yml
Copied!
will output
1
-
dog
:
woof
2
-
cat
:
meow
Copied!
Relative append
Given a sample.yml file of:
1
a
:
2
a1
:
3
b
:
4
-
cat
5
a2
:
6
b
:
7
-
dog
8
a3
:
{}
Copied!
then
1
yq
'.a[].b += ["mouse"]'
sample.yml
Copied!
will output
1
a
:
2
a1
:
3
b
:
4
-
cat
5
-
mouse
6
a2
:
7
b
:
8
-
dog
9
-
mouse
10
a3
:
11
b
:
12
-
mouse
Copied!
String concatenation
Given a sample.yml file of:
1
a
:
cat
2
b
:
meow
Copied!
then
1
yq
'.a += .b'
sample.yml
Copied!
will output
1
a
:
catmeow
2
b
:
meow
Copied!
Number addition - 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.9
Copied!
then
1
yq
'.a = .a + .b'
sample.yml
Copied!
will output
1
a
:
7.9
2
b
:
4.9
Copied!
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:
1
a
:
3
2
b
:
4
Copied!
then
1
yq
'.a = .a + .b'
sample.yml
Copied!
will output
1
a
:
7
2
b
:
4
Copied!
Increment 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
:
4
2
b
:
6
Copied!
Date addition
You can add durations to dates. Assumes RFC3339 date time format, see
date-time operators
for more information.
Given a sample.yml file of:
1
a
:
2021-01-01T00:00:00Z
Copied!
then
1
yq
'.a += "3h10m"'
sample.yml
Copied!
will output
1
a
:
2021-01-01T03:10:00Z
Copied!
Date addition - custom format
You can add durations to dates. See
date-time operators
for more information.
Given a sample.yml file of:
1
a
:
Saturday
,
15
-
Dec
-
01 at 2
:
59AM 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 6
:
00AM GMT
Copied!
Add to null
Adding to null simply returns the rhs
Running
1
yq --null-input
'null + "cat"'
Copied!
will output
1
cat
Copied!
Add maps to shallow merge
Adding objects together shallow merges them. Use
*
to deeply merge.
Given a sample.yml file of:
1
a
:
2
thing
:
3
name
:
Astuff
4
value
:
x
5
a1
:
cool
6
b
:
7
thing
:
8
name
:
Bstuff
9
legs
:
3
10
b1
:
neat
Copied!
then
1
yq
'.a += .b'
sample.yml
Copied!
will output
1
a
:
2
thing
:
3
name
:
Bstuff
4
legs
:
3
5
a1
:
cool
6
b1
:
neat
7
b
:
8
thing
:
9
name
:
Bstuff
10
legs
:
3
11
b1
:
neat
Copied!
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:
1
a
:
!horse
cat
2
b
:
!goat
_meow
Copied!
then
1
yq
'.a += .b'
sample.yml
Copied!
will output
1
a
:
!horse
cat_meow
2
b
:
!goat
_meow
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
1.2
2
b
:
!goat
2.3
Copied!
then
1
yq
'.a += .b'
sample.yml
Copied!
will output
1
a
:
!horse
3.5
2
b
:
!goat
2.3
Copied!
Previous
Operators
Next
Alternative (Default value)
Last modified
2mo ago
Copy link
Contents
Concatenate arrays
Concatenate to existing array
Concatenate null to array
Append 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