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
  • Update and style
  • Update multiple deeply nested properties
  • Update array elements relatively

Was this helpful?

  1. Operators

With

Use the with operator to conveniently make multiple updates to a deeply nested path, or to update array elements relatively to each other. The first argument expression sets the root context, and the second expression runs against that root context.

Update and style

Given a sample.yml file of:

a:
  deeply:
    nested: value

then

yq 'with(.a.deeply.nested; . = "newValue" | . style="single")' sample.yml

will output

a:
  deeply:
    nested: 'newValue'

Update multiple deeply nested properties

Given a sample.yml file of:

a:
  deeply:
    nested: value
    other: thing

then

yq 'with(.a.deeply; .nested = "newValue" | .other= "newThing")' sample.yml

will output

a:
  deeply:
    nested: newValue
    other: newThing

Update array elements relatively

The second expression runs with each element of the array as it's contextual root. This allows you to make updates relative to the element.

Given a sample.yml file of:

myArray:
  - a: apple
  - a: banana

then

yq 'with(.myArray[]; .b = .a + " yum")' sample.yml

will output

myArray:
  - a: apple
    b: apple yum
  - a: banana
    b: banana yum
PreviousVariable OperatorsNextOutput format

Last updated 2 years ago

Was this helpful?