mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 22:35:37 +00:00
Compare commits
2 Commits
19118c5552
...
500ee98a48
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
500ee98a48 | ||
|
|
785ee2cabb |
21
README.md
21
README.md
@ -4,27 +4,6 @@ a lightweight and portable command-line YAML processor. `yq` uses [jq](https://g
|
|||||||
|
|
||||||
yq is written in go - so you can download a dependency free binary for your platform and you are good to go! If you prefer there are a variety of package managers that can be used as well as docker, all listed below.
|
yq is written in go - so you can download a dependency free binary for your platform and you are good to go! If you prefer there are a variety of package managers that can be used as well as docker, all listed below.
|
||||||
|
|
||||||
## Notice for v4.x versions prior to 4.18.1
|
|
||||||
Since 4.18.1, yq's 'eval/e' command is the _default_ command and no longers needs to be specified.
|
|
||||||
|
|
||||||
Older versions will still need to specify 'eval/e'.
|
|
||||||
|
|
||||||
Similarly, '-' is no longer required as a filename to read from STDIN (unless reading from multiple files).
|
|
||||||
|
|
||||||
TLDR:
|
|
||||||
|
|
||||||
Prior to 4.18.1
|
|
||||||
```bash
|
|
||||||
cat file.yaml | yq e '.cool' -
|
|
||||||
```
|
|
||||||
|
|
||||||
4.18+
|
|
||||||
```bash
|
|
||||||
cat file.yaml | yq '.cool'
|
|
||||||
```
|
|
||||||
|
|
||||||
When merging multiple files together, `eval-all/ea` is still required to tell `yq` to run the expression against all the documents at once.
|
|
||||||
|
|
||||||
## Quick Usage Guide
|
## Quick Usage Guide
|
||||||
|
|
||||||
Read a value:
|
Read a value:
|
||||||
|
|||||||
@ -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.
|
||||||
|
|
||||||
|
|||||||
@ -12,8 +12,8 @@ diff file1.yml file2.yml
|
|||||||
|
|
||||||
Note that `yq` does not yet consider anchors when sorting by keys - this may result in invalid yaml documents if you are using merge anchors.
|
Note that `yq` does not yet consider anchors when sorting by keys - this may result in invalid yaml documents if you are using merge anchors.
|
||||||
|
|
||||||
For more advanced sorting, using `to_entries` to convert the map to an array, then sort/process the array as you like (e.g. using `sort_by`) and convert back to a map using `from_entries`.
|
For more advanced sorting, you can use the [sort_by](https://mikefarah.gitbook.io/yq/operators/sort) function on a map, and give it a custom function like `sort_by(key | downcase)`.
|
||||||
See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-keys) for an example.
|
|
||||||
|
|
||||||
## Sort keys of map
|
## Sort keys of map
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
|
|||||||
@ -109,6 +109,46 @@ cool:
|
|||||||
- c: banana
|
- c: banana
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Sort a map
|
||||||
|
Sorting a map, by default this will sort by the values
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
y: b
|
||||||
|
z: a
|
||||||
|
x: c
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq 'sort' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
z: a
|
||||||
|
y: b
|
||||||
|
x: c
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sort a map by keys
|
||||||
|
Use sort_by to sort a map using a custom function
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
Y: b
|
||||||
|
z: a
|
||||||
|
x: c
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq 'sort_by(key | downcase)' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
x: c
|
||||||
|
Y: b
|
||||||
|
z: a
|
||||||
|
```
|
||||||
|
|
||||||
## Sort is stable
|
## Sort is stable
|
||||||
Note the order of the elements in unchanged when equal in sorting.
|
Note the order of the elements in unchanged when equal in sorting.
|
||||||
|
|
||||||
|
|||||||
@ -71,7 +71,7 @@ person.food[0] = pizza
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Encode properties - custom separator
|
## Encode properties - custom separator
|
||||||
Use the --properties-customer-separator flag to specify your own key/value separator.
|
Use the --properties-separator flag to specify your own key/value separator.
|
||||||
|
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
@ -89,7 +89,7 @@ emptyMap: []
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
yq -o=props --properties-customer-separator=" :@ " sample.yml
|
yq -o=props --properties-separator=" :@ " sample.yml
|
||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```properties
|
```properties
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user