yq/pkg/yqlib/doc/operators/parent.md

53 lines
620 B
Markdown
Raw Normal View History

2021-11-23 23:59:19 +00:00
# Parent
Parent simply returns the parent nodes of the matching nodes.
2021-11-23 23:16:48 +00:00
## Simple example
Given a sample.yml file of:
```yaml
a:
nested: cat
```
then
```bash
yq eval '.a.nested | parent' sample.yml
```
will output
```yaml
nested: cat
```
2021-11-23 23:59:19 +00:00
## Parent of nested matches
2021-11-23 23:16:48 +00:00
Given a sample.yml file of:
```yaml
a:
fruit: apple
2021-11-23 23:59:19 +00:00
name: bob
2021-11-23 23:16:48 +00:00
b:
fruit: banana
2021-11-23 23:59:19 +00:00
name: sam
2021-11-23 23:16:48 +00:00
```
then
```bash
yq eval '.. | select(. == "banana") | parent' sample.yml
```
will output
```yaml
fruit: banana
2021-11-23 23:59:19 +00:00
name: sam
2021-11-23 23:16:48 +00:00
```
## No parent
Given a sample.yml file of:
```yaml
{}
```
then
```bash
yq eval 'parent' sample.yml
```
will output
```yaml
```