For more complex scenarios, variables can be used to hold values of expression to be used in other expressions.
Given a sample.yml file of:
a: cat
then
yq eval '.a as $foo | $foo' sample.yml
will output
cat
Given a sample.yml file of:
- cat- dog
then
yq eval '.[] as $foo | $foo' sample.yml
will output
catdog
Example taken from jq​
Given a sample.yml file of:
"posts":- "title": Frist psot"author": anon- "title": A well-written article"author": person1"realnames":"anon": Anonymous Coward"person1": Person McPherson
then
yq eval '.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}' sample.yml
will output
title: Frist psotauthor: Anonymous Cowardtitle: A well-written articleauthor: Person McPherson