mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Added credit for merge by array example
This commit is contained in:
parent
4c7c0b83aa
commit
418bd1a806
8
merge-array-by-key.sh
Executable file
8
merge-array-by-key.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
# ./yq ea '.[]' examples/data*.yaml
|
||||
|
||||
|
||||
./yq ea '
|
||||
((.[] | {.name: .}) as $item ireduce ({}; . * $item )) as $uniqueMap
|
||||
| ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value)
|
||||
' examples/data*.yaml
|
@ -239,6 +239,8 @@ so that there are no duplicates. The second half converts that map back to an ar
|
||||
|
||||
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:
|
||||
```yaml
|
||||
|
@ -33,6 +33,8 @@ var mergeArraysObjectKeysText = `There are two parts of the complex expression.
|
||||
so that there are no duplicates. The second half converts that map back to an array.
|
||||
|
||||
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)
|
||||
`
|
||||
|
||||
var mergeExpression = `((.[] | {.a: .}) as $item ireduce ({}; . * $item )) as $uniqueMap
|
||||
|
Loading…
Reference in New Issue
Block a user