yq/pkg/yqlib/doc/Select.md

35 lines
543 B
Markdown
Raw Normal View History

2020-11-17 23:32:30 +00:00
Select is used to filter arrays and maps by a boolean expression.
2020-11-22 02:16:54 +00:00
## Select elements from array
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
- cat
- goat
- dog
```
then
```bash
yq eval '.[] | select(. == "*at")' sample.yml
```
will output
```yaml
cat
goat
```
2020-11-22 02:16:54 +00:00
## Select and update matching values in map
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
a: {things: cat, bob: goat, horse: dog}
'': null
2020-11-17 23:32:30 +00:00
```
then
```bash
2020-11-22 02:50:32 +00:00
yq eval '(.a.[] | select(. == "*at")) |= "rabbit"' sample.yml
2020-11-17 23:32:30 +00:00
```
will output
```yaml
2020-12-26 22:36:33 +00:00
a: {things: rabbit, bob: rabbit, horse: dog}
'': null
2020-11-17 23:32:30 +00:00
```