Select
Select is used to filter arrays and maps by a boolean expression.
Given a sample.yml file of:
- cat
- goat
- dog
then
yq '.[] | select(. == "*at")' sample.yml
will output
cat
goat
Given a sample.yml file of:
- go-kart
- goat
- dog
then
yq '.[] | select(. == "go*")' sample.yml
will output
go-kart
goat
Given a sample.yml file of:
- ago
- go
- meow
- going
then
yq '.[] | select(. == "*go*")' sample.yml
will output
ago
go
going
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
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
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
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
Last modified 6mo ago