mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
1.8 KiB
1.8 KiB
Select
Select is used to filter arrays and maps by a boolean expression.
Select elements from array using wildcard prefix
Given a sample.yml file of:
- cat
- goat
- dog
then
yq eval '.[] | 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 eval '.[] | 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 eval '.[] | select(. == "*go*")' sample.yml
will output
ago
go
going
Select 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_4
then
yq eval '.[] | 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 eval '.[] | 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 eval '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 eval '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.yml
will output
a:
things: rabbit
bob: rabbit
horse: dog