mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
fa6fac1a76
* Remove extra backtick * Reword explanation of update * Reword explanation of relative update * Change "remaple" to "remain" * Change "clovver" to "clobber" * Reword explanation of update for comment operators * Reword explanation of relative update for comment operators * Change "array" to "expression" * Change "the golangs" to "Golang's" * Change "golangs" to "Golang's" * Change "can durations" to "can add durations" * Change "array scalars" to "arrays" * Change "beit" to "be it" * Fix typo in `eval` tip * Fix typo in header for `has` operation * Add space before pipe in `line` operator example * Fix typos in explanation of deep array merges * Change "is now used" to "is now used." * Change "object," to "object." * Changes "indexes" to "indices" * Remove extraneous copied text from `..` article * Reword explanation of `...` operator * Change "your are" to "you are" * Add link to `string` operator docs in `select` article * Change "is a" to "parameter specifies" in `string` operators article * Change "new line" to "newline" * Change "golang regex" to "Golang's regex" * Change "golang" to "Golang" * Add period * Remove comma in `subtract` article * Remove duplicate number subtraction example * Remove comma in `traverse` operator article * Clarify use of brackets when `read`ing with special characters
140 lines
2.2 KiB
Markdown
140 lines
2.2 KiB
Markdown
# Select
|
|
|
|
Select is used to filter arrays and maps by a boolean expression.
|
|
|
|
## Related Operators
|
|
|
|
- equals / not equals (`==`, `!=`) operators [here](https://mikefarah.gitbook.io/yq/operators/equals)
|
|
- comparison (`>=`, `<` etc) operators [here](https://mikefarah.gitbook.io/yq/operators/compare)
|
|
- boolean operators (`and`, `or`, `any` etc) [here](https://mikefarah.gitbook.io/yq/operators/boolean-operators)
|
|
|
|
## Select elements from array using wildcard prefix
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
- cat
|
|
- goat
|
|
- dog
|
|
```
|
|
then
|
|
```bash
|
|
yq '.[] | select(. == "*at")' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
cat
|
|
goat
|
|
```
|
|
|
|
## Select elements from array using wildcard suffix
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
- go-kart
|
|
- goat
|
|
- dog
|
|
```
|
|
then
|
|
```bash
|
|
yq '.[] | select(. == "go*")' sample.yml
|
|
```
|
|
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
|
|
yq '.[] | select(. == "*go*")' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
ago
|
|
go
|
|
going
|
|
```
|
|
|
|
## Select elements from array with regular expression
|
|
See more regular expression examples under the [`string` operator docs](https://mikefarah.gitbook.io/yq/operators/string-operators).
|
|
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
- this_0
|
|
- not_this
|
|
- nor_0_this
|
|
- thisTo_4
|
|
```
|
|
then
|
|
```bash
|
|
yq '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
this_0
|
|
thisTo_4
|
|
```
|
|
|
|
## Select items from a map
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
things: cat
|
|
bob: goat
|
|
horse: dog
|
|
```
|
|
then
|
|
```bash
|
|
yq '.[] | select(. == "cat" or test("og$"))' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
cat
|
|
dog
|
|
```
|
|
|
|
## Use select and with_entries to filter map keys
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
name: bob
|
|
legs: 2
|
|
game: poker
|
|
```
|
|
then
|
|
```bash
|
|
yq 'with_entries(select(.key | test("ame$")))' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
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:
|
|
```yaml
|
|
a:
|
|
things: cat
|
|
bob: goat
|
|
horse: dog
|
|
```
|
|
then
|
|
```bash
|
|
yq '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
a:
|
|
things: rabbit
|
|
bob: rabbit
|
|
horse: dog
|
|
```
|
|
|