Yq: find array element by value

Created on 11 Feb 2020  路  5Comments  路  Source: mikefarah/yq

I have this yaml structure:

animals:
  - cat
  - dog
  - rat

I want to uise yq to find out, if there is a specific animal (i.e. cat)
I expected something like this:
yq read file.yaml 'animals==cat'

but this does not work. is there any way to find out, if there is a arrayelement named "cat" (and print cat or nothing when not found)

enhancement

Most helpful comment

Not at the moment :(
the find array by element currently only works for arrays of maps. Need to come up with syntax for finding an element in an array of scalars.

perhaps:

yq r file.yaml 'animals(*==cat)'

All 5 comments

Not at the moment :(
the find array by element currently only works for arrays of maps. Need to come up with syntax for finding an element in an array of scalars.

perhaps:

yq r file.yaml 'animals(*==cat)'

It will be a big improvement !!
the ability to select an item is really powerful, like select and pipe in jq

Note that I used a '.' instead of '*' as wildcards already mean something :)

yq r file.yaml 'animals(.==cat)'

Ok Thanks ;o)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cdaguerre picture cdaguerre  路  4Comments

nicholasdille picture nicholasdille  路  7Comments

brunowego picture brunowego  路  5Comments

OnkelTem picture OnkelTem  路  8Comments

curiositycasualty picture curiositycasualty  路  5Comments