yq/pkg/yqlib/doc/Variable Operators.md
2021-02-11 10:58:40 +11:00

1.0 KiB

For more complex scenarios, variables can be used to hold values of expression to be used in other expressions.

Single value variable

Given a sample.yml file of:

a: cat

then

yq eval '.a as $foo | $foo' sample.yml

will output

cat

Multi value variable

Given a sample.yml file of:

- cat
- dog

then

yq eval '.[] as $foo | $foo' sample.yml

will output

cat
dog

Using variables as a lookup

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 psot
author: Anonymous Coward
title: A well-written article
author: Person McPherson