2021-11-24 00:15:42 +00:00
|
|
|
# Parent
|
|
|
|
|
|
|
|
Parent simply returns the parent nodes of the matching nodes.
|
|
|
|
|
2022-02-06 03:41:27 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2021-11-24 00:15:42 +00:00
|
|
|
## Simple example
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
nested: cat
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '.a.nested | parent' sample.yml
|
2021-11-24 00:15:42 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
nested: cat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Parent of nested matches
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
fruit: apple
|
|
|
|
name: bob
|
|
|
|
b:
|
|
|
|
fruit: banana
|
|
|
|
name: sam
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '.. | select(. == "banana") | parent' sample.yml
|
2021-11-24 00:15:42 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
fruit: banana
|
|
|
|
name: sam
|
|
|
|
```
|
|
|
|
|
|
|
|
## No parent
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
{}
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'parent' sample.yml
|
2021-11-24 00:15:42 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
```
|
|
|
|
|