mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-05 00:54:51 +08:00
Update document generation script
This commit is contained in:
+1
-30
@@ -5,112 +5,82 @@ The path operator can be used to get the traversal paths of matching nodes in an
|
||||
You can get the key/index of matching nodes by using the `path` operator to return the path array then piping that through `.[-1]` to get the last element of that array, the key.
|
||||
|
||||
## 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
|
||||
```
|
||||
|
||||
## Get map key
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
a:
|
||||
b: cat
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.a.b | path | .[-1]' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
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
|
||||
```
|
||||
|
||||
## Get array index
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
a:
|
||||
- cat
|
||||
- dog
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.a.[] | select(. == "dog") | path | .[-1]' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
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
|
||||
@@ -121,3 +91,4 @@ will output
|
||||
- 2
|
||||
value: frog
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user