Compare commits

...

2 Commits

Author SHA1 Message Date
Mike Farah
500ee98a48 Removing old notice 2025-03-03 15:23:47 +11:00
Mike Farah
785ee2cabb Updating docs 2025-03-03 15:12:22 +11:00
5 changed files with 68 additions and 25 deletions

View File

@ -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.
## 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
Read a value:

View File

@ -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.
Use `with_entries(op)` as a syntatic sugar for doing `to_entries | op | from_entries`.
## to_entries Map
Given a sample.yml file of:
```yaml
@ -101,6 +103,28 @@ KEY_a: 1
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
Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.

View File

@ -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.
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`.
See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-keys) for an example.
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)`.
## Sort keys of map
Given a sample.yml file of:

View File

@ -109,6 +109,46 @@ cool:
- 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
Note the order of the elements in unchanged when equal in sorting.

View File

@ -71,7 +71,7 @@ person.food[0] = pizza
```
## 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:
```yaml
@ -89,7 +89,7 @@ emptyMap: []
```
then
```bash
yq -o=props --properties-customer-separator=" :@ " sample.yml
yq -o=props --properties-separator=" :@ " sample.yml
```
will output
```properties