yq/pkg/yqlib/doc/operators/variable-operators.md
Christoph Stäbler dee592eb3b
Fix typo (#1222)
2022-05-24 18:18:27 +10:00

1.9 KiB

Variable Operators

Like the jq equivalents, variables are sometimes required for the more complex expressions (or swapping values between fields).

Note that there is also an additional ref operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path.

{% hint style="warning" %} Note that versions prior to 4.18 require the 'eval/e' command to be specified.

yq e <exp> <file> {% endhint %}

Single value variable

Given a sample.yml file of:

a: cat

then

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

will output

cat

Multi value variable

Given a sample.yml file of:

- cat
- dog

then

yq '.[] 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": First post
    "author": anon
  - "title": A well-written article
    "author": person1
"realnames":
  "anon": Anonymous Coward
  "person1": Person McPherson

then

yq '.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}' sample.yml

will output

title: First post
author: Anonymous Coward
title: A well-written article
author: Person McPherson

Using variables to swap values

Given a sample.yml file of:

a: a_value
b: b_value

then

yq '.a as $x  | .b as $y | .b = $x | .a = $y' sample.yml

will output

a: b_value
b: a_value

Use ref to reference a path repeatedly

Note: You may find the with operator more useful.

Given a sample.yml file of:

a:
  b: thing
  c: something

then

yq '.a.b ref $x | $x = "new" | $x style="double"' sample.yml

will output

a:
  b: "new"
  c: something