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
  • Single value variable
  • Multi value variable
  • Using variables as a lookup
  • Using variables to swap values
  • Use ref to reference a path repeatedly

Was this helpful?

  1. Operators

Variable Operators

Like the jq equivalents, variables are sometimes required for the more complex expressions (or swapping values between fields).

Note that there is also an additional ref operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path.

Single value variable

Given a sample.yml file of:

a: cat

then

yq '.a as $foo | $foo' sample.yml

will output

cat

Multi value variable

Given a sample.yml file of:

- cat
- dog

then

yq '.[] as $foo | $foo' sample.yml

will output

cat
dog

Using variables as a lookup

Given a sample.yml file of:

"posts":
  - "title": First post
    "author": anon
  - "title": A well-written article
    "author": person1
"realnames":
  "anon": Anonymous Coward
  "person1": Person McPherson

then

yq '.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}' sample.yml

will output

title: First post
author: Anonymous Coward
title: A well-written article
author: Person McPherson

Using variables to swap values

Given a sample.yml file of:

a: a_value
b: b_value

then

yq '.a as $x  | .b as $y | .b = $x | .a = $y' sample.yml

will output

a: b_value
b: a_value

Use ref to reference a path repeatedly

Note: You may find the with operator more useful.

Given a sample.yml file of:

a:
  b: thing
  c: something

then

yq '.a.b ref $x | $x = "new" | $x style="double"' sample.yml

will output

a:
  b: "new"
  c: something
PreviousUniqueNextWith

Last updated 2 years ago

Was this helpful?

Example taken from

jq