yq/pkg/yqlib/doc/Select.md

39 lines
529 B
Markdown
Raw Permalink 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
a:
things: cat
bob: goat
horse: dog
```
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
a:
things: rabbit
bob: rabbit
horse: dog
```