y
y
yq
Search…
Pick
Filter a map by the specified list of keys. Map is returned with the key in the order of the pick list.
Similarly, filter an array by the specified list of indices.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Pick keys from map

Note that the order of the keys matches the pick order and non existent keys are skipped.
Given a sample.yml file of:
1
myMap:
2
cat: meow
3
dog: bark
4
thing: hamster
5
hamster: squeek
Copied!
then
1
yq '.myMap |= pick(["hamster", "cat", "goat"])' sample.yml
Copied!
will output
1
myMap:
2
hamster: squeek
3
cat: meow
Copied!

Pick indices from array

Note that the order of the indexes matches the pick order and non existent indexes are skipped.
Given a sample.yml file of:
1
- cat
2
- leopard
3
- lion
Copied!
then
1
yq 'pick([2, 0, 734, -5])' sample.yml
Copied!
will output
1
- lion
2
- cat
Copied!
Last modified 2mo ago