mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
added path operator!
This commit is contained in:
59
pkg/yqlib/doc/Path Operators.md
Normal file
59
pkg/yqlib/doc/Path Operators.md
Normal file
@@ -0,0 +1,59 @@
|
||||
The path operator can be used to find 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.
|
||||
## Examples
|
||||
### Map path
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
a:
|
||||
b: cat
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.a.b | path' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
- a
|
||||
- b
|
||||
```
|
||||
|
||||
### Array path
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
a:
|
||||
- cat
|
||||
- dog
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.a.[] | select(. == "dog") | path' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
- a
|
||||
- 1
|
||||
```
|
||||
|
||||
### Print path and value
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
a:
|
||||
- cat
|
||||
- dog
|
||||
- frog
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.a.[] | select(. == "*og") | [{"path":path, "value":.}]' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
- path:
|
||||
- a
|
||||
- 1
|
||||
value: dog
|
||||
- path:
|
||||
- a
|
||||
- 2
|
||||
value: frog
|
||||
```
|
||||
|
||||
1
pkg/yqlib/doc/headers/Path Operators.md
Normal file
1
pkg/yqlib/doc/headers/Path Operators.md
Normal file
@@ -0,0 +1 @@
|
||||
The path operator can be used to find 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.
|
||||
Reference in New Issue
Block a user