yq/pkg/yqlib/doc/Path.md
2020-11-22 13:16:54 +11:00

798 B

The path operator can be used to get the traversal paths of matching nodes in an expression. The path is returned as an array, which if traversed in order will lead to the matching node.

Map path

Given a sample.yml file of:

a:
  b: cat

then

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

will output

- a
- b

Array path

Given a sample.yml file of:

a:
  - cat
  - dog

then

yq eval '.a.[] | select(. == "dog") | path' sample.yml

will output

- a
- 1

Print path and value

Given a sample.yml file of:

a:
  - cat
  - dog
  - frog

then

yq eval '.a.[] | select(. == "*og") | [{"path":path, "value":.}]' sample.yml

will output

- path:
    - a
    - 1
  value: dog
- path:
    - a
    - 2
  value: frog