pkg/yqlib/doc/operators/first.md
Returns the first matching element in an array, or first matching value in a map.
Can be given an expression to match with, otherwise will just return the first.
Given a sample.yml file of:
- a: banana
- a: cat
- a: apple
then
yq 'first(.a == "cat")' sample.yml
will output
a: cat
Given a sample.yml file of:
- a: banana
- a: cat
b: firstCat
- a: apple
- a: cat
b: secondCat
then
yq 'first(.a == "cat")' sample.yml
will output
a: cat
b: firstCat
Given a sample.yml file of:
- a: 10
- a: 100
- a: 1
- a: 101
then
yq 'first(.a > 50)' sample.yml
will output
a: 100
Given a sample.yml file of:
- a: false
- a: true
b: firstTrue
- a: false
- a: true
b: secondTrue
then
yq 'first(.a == true)' sample.yml
will output
a: true
b: firstTrue
Given a sample.yml file of:
- a: null
- a: cat
- a: apple
then
yq 'first(.a != null)' sample.yml
will output
a: cat
Given a sample.yml file of:
- a: dog
b: 7
- a: cat
b: 3
- a: apple
b: 5
then
yq 'first(.b > 4 and .b < 6)' sample.yml
will output
a: apple
b: 5
Given a sample.yml file of:
x:
a: banana
y:
a: cat
z:
a: apple
then
yq 'first(.a == "cat")' sample.yml
will output
a: cat
Given a sample.yml file of:
x:
a: 10
y:
a: 100
z:
a: 101
then
yq 'first(.a > 50)' sample.yml
will output
a: 100
Given a sample.yml file of:
items:
- a: banana
- a: cat
- a: apple
then
yq '.items | first(.a == "cat")' sample.yml
will output
a: cat
Given a sample.yml file of:
- a: banana
- a: cat
- a: apple
then
yq 'first(.a == "dog")' sample.yml
will output
Given a sample.yml file of:
[]
then
yq 'first(.a == "cat")' sample.yml
will output
Given a sample.yml file of:
hello
then
yq 'first(. == "hello")' sample.yml
will output
Given a sample.yml file of:
null
then
yq 'first(. == "hello")' sample.yml
will output
Given a sample.yml file of:
- a: banana
- a: cat
- a: apple
then
yq 'first(.a | test("^c"))' sample.yml
will output
a: cat
Given a sample.yml file of:
- a: hi
- a: hello
- a: world
then
yq 'first(.a | length > 4)' sample.yml
will output
a: hello
Given a sample.yml file of:
- banana
- cat
- apple
then
yq 'first(. == "cat")' sample.yml
will output
cat
Given a sample.yml file of:
- 10
- 100
- 1
then
yq 'first(. > 50)' sample.yml
will output
100
Given a sample.yml file of:
- 10
- 100
- 1
then
yq 'first' sample.yml
will output
10
Given a sample.yml file of:
- a: 10
- a: 100
then
yq 'first' sample.yml
will output
a: 10