mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 19:35:38 +00:00
Better merge array by key example, fixed spelling mistakes
This commit is contained in:
parent
61f561a9e0
commit
fd3ee46ccf
@ -108,7 +108,7 @@ will output
|
|||||||
a: frog
|
a: frog
|
||||||
```
|
```
|
||||||
|
|
||||||
## Two non existant keys are equal
|
## Two non existent keys are equal
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
a: frog
|
a: frog
|
||||||
|
|||||||
@ -234,10 +234,12 @@ will output
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Merge arrays of objects together, matching on a key
|
## Merge arrays of objects together, matching on a key
|
||||||
It's a complex command, the trickyness comes from needing to have the right context in the expressions.
|
There are two parts of the complex expression. The first part is doing the hard work, it creates a map from the arrays keyed by '.a',
|
||||||
First we save the second array into a variable '$two' which lets us reference it later.
|
so that there are no duplicates. The second half converts that map back to an array.
|
||||||
We then need to update the first array. We will use the relative update (|=) because we need to update relative to the current element of the array in the LHS in the RHS expression.
|
|
||||||
We set the current element of the first array as $cur. Now we multiply (merge) $cur with the matching entry in $two, by passing $two through a select filter.
|
To use this, you will need to update '.[]' to be the expression to your array (e.g. .my.array[]), and '.a' to be the key field of your array (e.g. '.name')
|
||||||
|
|
||||||
|
Thanks Kev from [stackoverflow](https://stackoverflow.com/a/70109529/1168223)
|
||||||
|
|
||||||
|
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
@ -260,7 +262,9 @@ And another sample another.yml file of:
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
yq eval-all '(select(fi==1) | .[]) as $two | select(fi==0) | .[] |= (. as $cur | $cur * ($two | select(.a == $cur.a)))' sample.yml another.yml
|
yq eval-all '((.[] | {.a: .}) as $item ireduce ({}; . * $item )) as $uniqueMap
|
||||||
|
| ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value)
|
||||||
|
' sample.yml another.yml
|
||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
@ -271,6 +275,8 @@ will output
|
|||||||
- a: banana
|
- a: banana
|
||||||
b: bananaB
|
b: bananaB
|
||||||
c: bananaC
|
c: bananaC
|
||||||
|
- a: dingo
|
||||||
|
c: dingoC
|
||||||
```
|
```
|
||||||
|
|
||||||
## Merge to prefix an element
|
## Merge to prefix an element
|
||||||
|
|||||||
@ -209,7 +209,7 @@ will output
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Test using regex
|
## Test using regex
|
||||||
Like jq'q equivalant, this works like match but only returns true/false instead of full match details
|
Like jq'q equivalent, this works like match but only returns true/false instead of full match details
|
||||||
|
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user