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
  • Pick keys from map
  • Pick keys from map, included all the keys
  • Pick indices from array

Was this helpful?

  1. Operators

Pick

Filter a map by the specified list of keys. Map is returned with the key in the order of the pick list.

Similarly, filter an array by the specified list of indices.

Pick keys from map

Note that the order of the keys matches the pick order and non existent keys are skipped.

Given a sample.yml file of:

myMap:
  cat: meow
  dog: bark
  thing: hamster
  hamster: squeak

then

yq '.myMap |= pick(["hamster", "cat", "goat"])' sample.yml

will output

myMap:
  hamster: squeak
  cat: meow

Pick keys from map, included all the keys

We create a map of the picked keys plus all the current keys, and run that through unique

Given a sample.yml file of:

myMap:
  cat: meow
  dog: bark
  thing: hamster
  hamster: squeak

then

yq '.myMap |= pick( (["thing"] + keys) | unique)' sample.yml

will output

myMap:
  thing: hamster
  cat: meow
  dog: bark
  hamster: squeak

Pick indices from array

Note that the order of the indices matches the pick order and non existent indices are skipped.

Given a sample.yml file of:

- cat
- leopard
- lion

then

yq 'pick([2, 0, 734, -5])' sample.yml

will output

- lion
- cat
PreviousPathNextPipe

Last updated 9 months ago

Was this helpful?