y
y
yq
Search…
Select
Select is used to filter arrays and maps by a boolean expression.

Related Operators

  • equals / not equals (==, !=) operators here
  • comparison (>=, < etc) operators here
  • boolean operators (and, or, any etc) here
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Select elements from array using wildcard prefix

Given a sample.yml file of:
1
- cat
2
- goat
3
- dog
Copied!
then
1
yq '.[] | select(. == "*at")' sample.yml
Copied!
will output
1
cat
2
goat
Copied!

Select elements from array using wildcard suffix

Given a sample.yml file of:
1
- go-kart
2
- goat
3
- dog
Copied!
then
1
yq '.[] | select(. == "go*")' sample.yml
Copied!
will output
1
go-kart
2
goat
Copied!

Select elements from array using wildcard prefix and suffix

Given a sample.yml file of:
1
- ago
2
- go
3
- meow
4
- going
Copied!
then
1
yq '.[] | select(. == "*go*")' sample.yml
Copied!
will output
1
ago
2
go
3
going
Copied!

Select elements from array with regular expression

See more regular expression examples under the string operator docs.
Given a sample.yml file of:
1
- this_0
2
- not_this
3
- nor_0_this
4
- thisTo_4
Copied!
then
1
yq '.[] | select(test("[a-zA-Z]+_[0-9]quot;))' sample.yml
Copied!
will output
1
this_0
2
thisTo_4
Copied!

Select items from a map

Given a sample.yml file of:
1
things: cat
2
bob: goat
3
horse: dog
Copied!
then
1
yq '.[] | select(. == "cat" or test("ogquot;))' sample.yml
Copied!
will output
1
cat
2
dog
Copied!

Use select and with_entries to filter map keys

Given a sample.yml file of:
1
name: bob
2
legs: 2
3
game: poker
Copied!
then
1
yq 'with_entries(select(.key | test("amequot;)))' sample.yml
Copied!
will output
1
name: bob
2
game: poker
Copied!

Select multiple items in a map and update

Note the brackets around the entire LHS.
Given a sample.yml file of:
1
a:
2
things: cat
3
bob: goat
4
horse: dog
Copied!
then
1
yq '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.yml
Copied!
will output
1
a:
2
things: rabbit
3
bob: rabbit
4
horse: dog
Copied!
Last modified 3mo ago