mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-09 16:09:07 +00:00
Compare commits
4 Commits
be36f93921
...
2a68a61354
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a68a61354 | ||
|
|
ba1b9737b6 | ||
|
|
fe82725be8 | ||
|
|
f5e4316e84 |
10
README.md
10
README.md
@ -76,21 +76,21 @@ For instance, VERSION=v4.2.0 and BINARY=yq_linux_amd64
|
|||||||
#### Compressed via tar.gz
|
#### Compressed via tar.gz
|
||||||
```bash
|
```bash
|
||||||
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY}.tar.gz -O - |\
|
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY}.tar.gz -O - |\
|
||||||
tar xz && mv ${BINARY} /usr/bin/yq
|
tar xz && mv ${BINARY} /usr/local/bin/yq
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Plain binary
|
#### Plain binary
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY} -O /usr/bin/yq &&\
|
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY} -O /usr/local/bin/yq &&\
|
||||||
chmod +x /usr/bin/yq
|
chmod +x /usr/local/bin/yq
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Latest version
|
#### Latest version
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq &&\
|
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq &&\
|
||||||
chmod +x /usr/bin/yq
|
chmod +x /usr/local/bin/yq
|
||||||
```
|
```
|
||||||
|
|
||||||
### MacOS / Linux via Homebrew:
|
### MacOS / Linux via Homebrew:
|
||||||
|
|||||||
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
Similar to the same named functions in `jq` these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.
|
Similar to the same named functions in `jq` these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.
|
||||||
|
|
||||||
|
Use `with_entries(op)` as a syntatic sugar for doing `to_entries | op | from_entries`.
|
||||||
|
|
||||||
## to_entries Map
|
## to_entries Map
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
@ -101,6 +103,28 @@ KEY_a: 1
|
|||||||
KEY_b: 2
|
KEY_b: 2
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Use with_entries to update keys recursively
|
||||||
|
We use (.. | select(tag="map")) to find all the maps in the doc, then |= to update each one of those maps. In the update, with_entries is used.
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
a: 1
|
||||||
|
b:
|
||||||
|
b_a: nested
|
||||||
|
b_b: thing
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq '(.. | select(tag=="!!map")) |= with_entries(.key |= "KEY_" + .)' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
KEY_a: 1
|
||||||
|
KEY_b:
|
||||||
|
KEY_b_a: nested
|
||||||
|
KEY_b_b: thing
|
||||||
|
```
|
||||||
|
|
||||||
## Custom sort map keys
|
## Custom sort map keys
|
||||||
Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.
|
Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
# Entries
|
# Entries
|
||||||
|
|
||||||
Similar to the same named functions in `jq` these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.
|
Similar to the same named functions in `jq` these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.
|
||||||
|
|
||||||
|
Use `with_entries(op)` as a syntatic sugar for doing `to_entries | op | from_entries`.
|
||||||
|
|||||||
@ -110,7 +110,7 @@ cool:
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Sort a map
|
## Sort a map
|
||||||
Sorting a map, by default, will sort by the values
|
Sorting a map, by default this will sort by the values
|
||||||
|
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@ -72,6 +72,15 @@ var entriesOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n",
|
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Use with_entries to update keys recursively",
|
||||||
|
document: `{a: 1, b: {b_a: nested, b_b: thing}}`,
|
||||||
|
expression: `(.. | select(tag=="!!map")) |= with_entries(.key |= "KEY_" + .)`,
|
||||||
|
subdescription: "We use (.. | select(tag=\"map\")) to find all the maps in the doc, then |= to update each one of those maps. In the update, with_entries is used.",
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!map)::{KEY_a: 1, KEY_b: {KEY_b_a: nested, KEY_b_b: thing}}\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
description: "Use with_entries to update keys comment",
|
description: "Use with_entries to update keys comment",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user