2021-11-03 04:00:58 +00:00
|
|
|
# Select
|
|
|
|
|
|
|
|
Select is used to filter arrays and maps by a boolean expression.
|
|
|
|
|
2022-02-06 03:39:46 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2022-01-15 07:18:52 +00:00
|
|
|
## Select elements from array using wildcard prefix
|
2021-11-03 04:00:58 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- cat
|
|
|
|
- goat
|
|
|
|
- dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | select(. == "*at")' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cat
|
|
|
|
goat
|
|
|
|
```
|
|
|
|
|
2022-01-15 07:18:52 +00:00
|
|
|
## Select elements from array using wildcard suffix
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- go-kart
|
|
|
|
- goat
|
|
|
|
- dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | select(. == "go*")' sample.yml
|
2022-01-15 07:18:52 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
go-kart
|
|
|
|
goat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Select elements from array using wildcard prefix and suffix
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- ago
|
|
|
|
- go
|
|
|
|
- meow
|
|
|
|
- going
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | select(. == "*go*")' sample.yml
|
2022-01-15 07:18:52 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
ago
|
|
|
|
go
|
|
|
|
going
|
|
|
|
```
|
|
|
|
|
2022-01-15 07:22:26 +00:00
|
|
|
## Select elements from array with regular expression
|
|
|
|
See more regular expression examples under the `string` operator docs.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- this_0
|
|
|
|
- not_this
|
|
|
|
- nor_0_this
|
|
|
|
- thisTo_4
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.yml
|
2022-01-15 07:22:26 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
this_0
|
|
|
|
thisTo_4
|
|
|
|
```
|
|
|
|
|
2022-01-15 07:18:52 +00:00
|
|
|
## Select items from a map
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
things: cat
|
|
|
|
bob: goat
|
|
|
|
horse: dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | select(. == "cat" or test("og$"))' sample.yml
|
2022-01-15 07:18:52 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cat
|
|
|
|
dog
|
|
|
|
```
|
|
|
|
|
2022-01-15 07:22:26 +00:00
|
|
|
## Use select and with_entries to filter map keys
|
2022-01-15 07:18:52 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
name: bob
|
|
|
|
legs: 2
|
|
|
|
game: poker
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'with_entries(select(.key | test("ame$")))' sample.yml
|
2022-01-15 07:18:52 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
name: bob
|
|
|
|
game: poker
|
|
|
|
```
|
|
|
|
|
|
|
|
## Select multiple items in a map and update
|
|
|
|
Note the brackets around the entire LHS.
|
|
|
|
|
2021-11-03 04:00:58 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
things: cat
|
|
|
|
bob: goat
|
|
|
|
horse: dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
things: rabbit
|
|
|
|
bob: rabbit
|
|
|
|
horse: dog
|
|
|
|
```
|
|
|
|
|