Given the yaml file test.yaml
:
- alpha
- bravo
- one
- two
- charlie
- delta
How do I find the i
th element in the array? For example:
i=2
Then array[$i]
should return charlie
.
I am using Mike Farah's yq
.
If I try
yq '.[$i]' test.yaml
I get not the i
th element in the array as expected, but rather the entire array! Output:
alpha
bravo - one - two
charlie
delta
What am I doing incorrectly?
Following the yq documentation for environment variable operators, we can use the variable $i
like this:
i=$i yq '.[env(i)]' test.yaml
# alpha