Select
Select is used to filter arrays and maps by a boolean expression.
Related Operators
equals / not equals (
==,!=) operators herecomparison (
>=,<etc) operators hereboolean operators (
and,or,anyetc) here
Select elements from array using wildcard prefix
Given a sample.yml file of:
- cat
- goat
- dogthen
yq '.[] | select(. == "*at")' sample.ymlwill output
cat
goatSelect elements from array using wildcard suffix
Given a sample.yml file of:
- go-kart
- goat
- dogthen
yq '.[] | select(. == "go*")' sample.ymlwill output
go-kart
goatSelect elements from array using wildcard prefix and suffix
Given a sample.yml file of:
- ago
- go
- meow
- goingthen
yq '.[] | select(. == "*go*")' sample.ymlwill output
ago
go
goingSelect elements from array with regular expression
See more regular expression examples under the string operator docs.
Given a sample.yml file of:
- this_0
- not_this
- nor_0_this
- thisTo_4then
yq '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.ymlwill output
this_0
thisTo_4Select items from a map
Given a sample.yml file of:
things: cat
bob: goat
horse: dogthen
yq '.[] | select(. == "cat" or test("og$"))' sample.ymlwill output
cat
dogUse select and with_entries to filter map keys
Given a sample.yml file of:
name: bob
legs: 2
game: pokerthen
yq 'with_entries(select(.key | test("ame$")))' sample.ymlwill output
name: bob
game: pokerSelect 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: dogthen
yq '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.ymlwill output
a:
things: rabbit
bob: rabbit
horse: dogLast updated
Was this helpful?