yq/pkg/yqlib/doc/Traverse (Read).md

405 lines
4.9 KiB
Markdown
Raw Normal View History

2020-11-22 02:16:54 +00:00
This is the simplest (and perhaps most used) operator, it is used to navigate deeply into yaml structurse.
## Simple map navigation
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
a:
b: apple
```
then
```bash
yq eval '.a' sample.yml
```
will output
```yaml
b: apple
```
2020-11-22 02:16:54 +00:00
## Splat
2020-11-17 23:32:30 +00:00
Often used to pipe children into other operators
Given a sample.yml file of:
```yaml
- b: apple
- c: banana
```
then
```bash
yq eval '.[]' sample.yml
```
will output
```yaml
b: apple
c: banana
```
2020-11-22 02:16:54 +00:00
## Special characters
2021-02-05 02:29:35 +00:00
Use quotes with brackets around path elements with special characters
2020-11-22 02:16:54 +00:00
Given a sample.yml file of:
```yaml
"{}": frog
```
then
```bash
2021-02-05 02:29:35 +00:00
yq eval '.["{}"]' sample.yml
2020-11-22 02:16:54 +00:00
```
will output
```yaml
frog
```
2021-01-09 01:06:19 +00:00
## Dynamic keys
Expressions within [] can be used to dynamically lookup / calculate keys
Given a sample.yml file of:
```yaml
b: apple
apple: crispy yum
banana: soft yum
```
then
```bash
yq eval '.[.b]' sample.yml
```
will output
```yaml
crispy yum
```
2020-11-22 02:16:54 +00:00
## Children don't exist
2020-11-17 23:32:30 +00:00
Nodes are added dynamically while traversing
Given a sample.yml file of:
```yaml
c: banana
```
then
```bash
yq eval '.a.b' sample.yml
```
will output
```yaml
null
```
2020-11-22 02:16:54 +00:00
## Wildcard matching
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
a:
cat: apple
mad: things
```
then
```bash
yq eval '.a."*a*"' sample.yml
```
will output
```yaml
apple
things
```
2020-11-22 02:16:54 +00:00
## Aliases
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
a: &cat
c: frog
b: *cat
```
then
```bash
yq eval '.b' sample.yml
```
will output
```yaml
*cat
```
2020-11-22 02:16:54 +00:00
## Traversing aliases with splat
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
a: &cat
c: frog
b: *cat
```
then
```bash
2020-12-27 12:00:46 +00:00
yq eval '.b[]' sample.yml
2020-11-17 23:32:30 +00:00
```
will output
```yaml
frog
```
2020-11-22 02:16:54 +00:00
## Traversing aliases explicitly
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
a: &cat
c: frog
b: *cat
```
then
```bash
yq eval '.b.c' sample.yml
```
will output
```yaml
frog
```
2020-11-22 02:16:54 +00:00
## Traversing arrays by index
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
- 1
- 2
- 3
```
then
```bash
yq eval '.[0]' sample.yml
2020-11-17 23:32:30 +00:00
```
will output
```yaml
1
```
2020-11-22 02:16:54 +00:00
## Maps with numeric keys
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
2: cat
```
then
```bash
yq eval '.[2]' sample.yml
2020-11-17 23:32:30 +00:00
```
will output
```yaml
cat
```
2020-11-22 02:16:54 +00:00
## Maps with non existing numeric keys
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
a: b
```
then
```bash
yq eval '.[0]' sample.yml
2020-11-17 23:32:30 +00:00
```
will output
```yaml
null
```
2020-11-22 02:16:54 +00:00
## Traversing merge anchors
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thing
```
then
```bash
yq eval '.foobar.a' sample.yml
```
will output
```yaml
foo_a
```
2020-11-22 02:16:54 +00:00
## Traversing merge anchors with override
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thing
```
then
```bash
yq eval '.foobar.c' sample.yml
```
will output
```yaml
foo_c
```
2020-11-22 02:16:54 +00:00
## Traversing merge anchors with local override
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thing
```
then
```bash
yq eval '.foobar.thing' sample.yml
```
will output
```yaml
foobar_thing
```
2020-11-22 02:16:54 +00:00
## Splatting merge anchors
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thing
```
then
```bash
2020-12-27 12:00:46 +00:00
yq eval '.foobar[]' sample.yml
2020-11-17 23:32:30 +00:00
```
will output
```yaml
foo_c
foo_a
foobar_thing
```
2020-11-22 02:16:54 +00:00
## Traversing merge anchor lists
2020-11-17 23:32:30 +00:00
Note that the later merge anchors override previous
Given a sample.yml file of:
```yaml
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thing
```
then
```bash
yq eval '.foobarList.thing' sample.yml
```
will output
```yaml
bar_thing
```
2020-11-22 02:16:54 +00:00
## Splatting merge anchor lists
2020-11-17 23:32:30 +00:00
Given a sample.yml file of:
```yaml
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thing
```
then
```bash
2020-12-27 12:00:46 +00:00
yq eval '.foobarList[]' sample.yml
2020-11-17 23:32:30 +00:00
```
will output
```yaml
bar_b
foo_a
bar_thing
foobarList_c
```
2020-12-26 10:37:08 +00:00
## Select multiple indices
Given a sample.yml file of:
```yaml
a:
- a
- b
- c
```
then
```bash
yq eval '.a[0, 2]' sample.yml
```
will output
```yaml
a
c
```