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
  • Related Operators
  • Select elements from array using wildcard prefix
  • Select elements from array using wildcard suffix
  • Select elements from array using wildcard prefix and suffix
  • Select elements from array with regular expression
  • Select items from a map
  • Use select and with_entries to filter map keys
  • Select multiple items in a map and update

Was this helpful?

  1. Operators

Select

PreviousReverseNextShuffle

Last updated 2 years ago

Was this helpful?

Select is used to filter arrays and maps by a boolean expression.

Related Operators

  • equals / not equals (==, !=) operators

  • comparison (>=, < etc) operators

  • boolean operators (and, or, any etc)

Select elements from array using wildcard prefix

Given a sample.yml file of:

- cat
- goat
- dog

then

yq '.[] | select(. == "*at")' sample.yml

will output

cat
goat

Select elements from array using wildcard suffix

Given a sample.yml file of:

- go-kart
- goat
- dog

then

yq '.[] | select(. == "go*")' sample.yml

will output

go-kart
goat

Select elements from array using wildcard prefix and suffix

Given a sample.yml file of:

- ago
- go
- meow
- going

then

yq '.[] | select(. == "*go*")' sample.yml

will output

ago
go
going

Select elements from array with regular expression

Given a sample.yml file of:

- this_0
- not_this
- nor_0_this
- thisTo_4

then

yq '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.yml

will output

this_0
thisTo_4

Select items from a map

Given a sample.yml file of:

things: cat
bob: goat
horse: dog

then

yq '.[] | select(. == "cat" or test("og$"))' sample.yml

will output

cat
dog

Use select and with_entries to filter map keys

Given a sample.yml file of:

name: bob
legs: 2
game: poker

then

yq 'with_entries(select(.key | test("ame$")))' sample.yml

will output

name: bob
game: poker

Select multiple items in a map and update

Note the brackets around the entire LHS.

Given a sample.yml file of:

a:
  things: cat
  bob: goat
  horse: dog

then

yq '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.yml

will output

a:
  things: rabbit
  bob: rabbit
  horse: dog

See more regular expression examples under the .

here
here
here
string operator docs