yq/pkg/yqlib/doc/Boolean Operators.md
Mike Farah bc87aca8d7 wip
2020-11-20 14:35:34 +11:00

1.9 KiB

The or and and operators take two parameters and return a boolean result. These are most commonly used with the select operator to filter particular nodes.

Examples

Update node to be the child value

Given a sample.yml file of:

a:
  b:
    g: foof

then

yq eval '.a |= .b' sample.yml

will output

a:
  g: foof

Update node to be the sibling value

Given a sample.yml file of:

a:
  b: child
b: sibling

then

yq eval '.a = .b' sample.yml

will output

a: sibling
b: sibling

Updated multiple paths

Given a sample.yml file of:

a: fieldA
b: fieldB
c: fieldC

then

yq eval '(.a, .c) |= "potatoe"' sample.yml

will output

a: potatoe
b: fieldB
c: potatoe

Update string value

Given a sample.yml file of:

a:
  b: apple

then

yq eval '.a.b = "frog"' sample.yml

will output

a:
  b: frog

Update string value via |=

Note there is no difference between = and |= when the RHS is a scalar

Given a sample.yml file of:

a:
  b: apple

then

yq eval '.a.b |= "frog"' sample.yml

will output

a:
  b: frog

Update selected results

Given a sample.yml file of:

a:
  b: apple
  c: cactus

then

yq eval '.a[] | select(. == "apple") |= "frog"' sample.yml

will output

a:
  b: frog
  c: cactus

Update array values

Given a sample.yml file of:

- candy
- apple
- sandy

then

yq eval '.[] | select(. == "*andy") |= "bogs"' sample.yml

will output

- bogs
- apple
- bogs

Update empty object

Given a sample.yml file of:

'': null

then

yq eval '.a.b |= "bogs"' sample.yml

will output

'': null
a:
  b: bogs

Update empty object and array

Given a sample.yml file of:

'': null

then

yq eval '.a.b[0] |= "bogs"' sample.yml

will output

'': null
a:
  b:
    - bogs