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
  • LHS is defined
  • LHS is not defined
  • LHS is null
  • LHS is false
  • RHS is an expression
  • Update or create - entity exists
  • Update or create - entity does not exist

Was this helpful?

  1. Operators

Alternative (Default value)

This operator is used to provide alternative (or default) values when a particular expression is either null or false.

LHS is defined

Given a sample.yml file of:

a: bridge

then

yq '.a // "hello"' sample.yml

will output

bridge

LHS is not defined

Given a sample.yml file of:

{}

then

yq '.a // "hello"' sample.yml

will output

hello

LHS is null

Given a sample.yml file of:

a: ~

then

yq '.a // "hello"' sample.yml

will output

hello

LHS is false

Given a sample.yml file of:

a: false

then

yq '.a // "hello"' sample.yml

will output

hello

RHS is an expression

Given a sample.yml file of:

a: false
b: cat

then

yq '.a // .b' sample.yml

will output

cat

Update or create - entity exists

This initialises a if it's not present

Given a sample.yml file of:

a: 1

then

yq '(.a // (.a = 0)) += 1' sample.yml

will output

a: 2

Update or create - entity does not exist

This initialises a if it's not present

Given a sample.yml file of:

b: camel

then

yq '(.a // (.a = 0)) += 1' sample.yml

will output

b: camel
a: 1
PreviousAddNextAnchor and Alias Operators

Last updated 2 years ago

Was this helpful?