mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-12 19:25:37 +00:00
Moving tip to recipe
This commit is contained in:
parent
b2965d58a6
commit
50adfee6ff
@ -59,6 +59,44 @@ will output
|
||||
- The expression `. + 1` increments the numBuckets counter.
|
||||
- See the [assign](https://mikefarah.gitbook.io/yq/operators/assign-update) and [add](https://mikefarah.gitbook.io/yq/operators/add) operators for more information.
|
||||
|
||||
## Deeply prune a tree
|
||||
Say we are only interested in child1 and child2, and want to filter everything else out.
|
||||
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
parentA:
|
||||
- bob
|
||||
parentB:
|
||||
child1: i am child1
|
||||
child3: hiya
|
||||
parentC:
|
||||
childX: cool
|
||||
child2: me child2
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq '(
|
||||
.. | # recurse through all the nodes
|
||||
select(has("child1") or has("child2")) | # match parents that have either child1 or child2
|
||||
(.child1, .child2) | # select those children
|
||||
select(.) # filter out nulls
|
||||
) as $i ireduce({}; # using that set of nodes, create a new result map
|
||||
setpath($i | path; $i) # and put in each node, using its original path
|
||||
)' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
parentB:
|
||||
child1: i am child1
|
||||
parentC:
|
||||
child2: me child2
|
||||
```
|
||||
|
||||
### Explanation:
|
||||
- Find all the matching child1 and child2 nodes
|
||||
- Using ireduce, create a new map using just those nodes
|
||||
- Set each node into the new map using its original path
|
||||
|
||||
## Multiple or complex updates to items in an array
|
||||
We have an array and we want to _update_ the elements with a particular name in reference to its type.
|
||||
|
||||
|
@ -138,10 +138,6 @@ yq ea '(.. lineComment |= filename + ":" + line) | select(fi==0) * select(fi==1
|
||||
See [here](https://mikefarah.gitbook.io/yq/operators/multiply-merge#merge-arrays-of-objects-together-matching-on-a-key) for a working example.
|
||||
|
||||
|
||||
## Deeply prune a tree
|
||||
|
||||
See [here](https://mikefarah.gitbook.io/yq/operators/path#set-path-to-prune-deep-paths) for a working example.
|
||||
|
||||
## Creating a new file / working with blank documents
|
||||
|
||||
To create a new `yaml` file simply:
|
||||
|
Loading…
Reference in New Issue
Block a user