This commit is contained in:
Mike Farah 2023-03-18 14:54:23 +11:00
parent f6fad90a48
commit 93adeb2d79
23 changed files with 217 additions and 54 deletions

View File

@ -27,6 +27,7 @@
* [Create, Collect into Object](operators/create-collect-into-object.md)
* [Date Time](operators/datetime.md)
* [Delete](operators/delete.md)
* [Divide](operators/divide.md)
* [Document Index](operators/document-index.md)
* [Encode / Decode](operators/encode-decode.md)
* [Entries](operators/entries.md)
@ -34,6 +35,7 @@
* [Equals](operators/equals.md)
* [Eval](operators/eval.md)
* [File Operators](operators/file-operators.md)
* [Filter Operator](operators/filter.md)
* [Flatten](operators/flatten.md)
* [Group By](operators/group-by.md)
* [Has](operators/has.md)
@ -42,6 +44,7 @@
* [Line](operators/line.md)
* [Load](operators/load.md)
* [Map](operators/map.md)
* [Modulo](operators/modulo.md)
* [Multiply (Merge)](operators/multiply-merge.md)
* [Parent](operators/parent.md)
* [Path](operators/path.md)

View File

@ -302,7 +302,7 @@ foobar:
```
## Dereference and update a field
`Use explode with multiply to dereference an object
Use explode with multiply to dereference an object
Given a sample.yml file of:
```yaml

View File

@ -3,10 +3,10 @@
This operator is used to update node values. It can be used in either the:
### plain form: `=`
Which will assign the LHS node values to the RHS node values. The RHS expression is run against the matching nodes in the pipeline.
Which will set the LHS node values equal to the RHS node values. The RHS expression is run against the matching nodes in the pipeline.
### relative form: `|=`
This will do a similar thing to the plain form, however, the RHS expression is run against _the LHS nodes_. This is useful for updating values based on old values, e.g. increment.
This will do a similar thing to the plain form, but the RHS expression is run with _each LHS node as context_. This is useful for updating values based on old values, e.g. increment.
### Flags
@ -203,7 +203,7 @@ a:
```
## Update node value that has an anchor
Anchor will remaple
Anchor will remain
Given a sample.yml file of:
```yaml
@ -250,7 +250,7 @@ a: !cat woof
b: !dog woof
```
## Custom types: clovver
## Custom types: clobber
Use the `c` option to clobber custom tags
Given a sample.yml file of:

View File

@ -5,10 +5,10 @@ Use these comment operators to set or retrieve comments. Note that line comments
Like the `=` and `|=` assign operators, the same syntax applies when updating comments:
### plain form: `=`
This will assign the LHS nodes comments to the expression on the RHS. The RHS is run against the matching nodes in the pipeline
This will set the LHS nodes' comments equal to the expression on the RHS. The RHS is run against the matching nodes in the pipeline
### relative form: `|=`
Similar to the plain form, however the RHS evaluates against each matching LHS node! This is useful if you want to set the comments as a relative expression of the node, for instance its value or path.
This is similar to the plain form, but it evaluates the RHS with _each matching LHS node as context_. This is useful if you want to set the comments as a relative expression of the node, for instance its value or path.
## Set line comment
Set the comment on the key node for more reliability (see below).

View File

@ -4,7 +4,7 @@ This returns `true` if the context contains the passed in parameter, and false o
{% hint style="warning" %}
_Note_ that, just like jq, when checking if an array of strings `contains` another, this will use `contains` and _not_ equals to check each string. This means an array like `contains(["cat"])` will return true for an array `["cats"]`.
_Note_ that, just like jq, when checking if an array of strings `contains` another, this will use `contains` and _not_ equals to check each string. This means an expression like `contains(["cat"])` will return true for an array `["cats"]`.
See the "Array has a subset array" example below on how to check for a subset.

View File

@ -3,7 +3,7 @@
Various operators for parsing and manipulating dates.
## Date time formattings
This uses the golangs built in time library for parsing and formatting date times.
This uses Golang's built in time library for parsing and formatting date times.
When not specified, the RFC3339 standard is assumed `2006-01-02T15:04:05Z07:00` for parsing.
@ -17,13 +17,13 @@ See the [library docs](https://pkg.go.dev/time#pkg-constants) for examples of fo
## Timezones
This uses golangs built in LoadLocation function to parse timezones strings. See the [library docs](https://pkg.go.dev/time#LoadLocation) for more details.
This uses Golang's built in LoadLocation function to parse timezones strings. See the [library docs](https://pkg.go.dev/time#LoadLocation) for more details.
## Durations
Durations are parsed using golangs built in [ParseDuration](https://pkg.go.dev/time#ParseDuration) function.
Durations are parsed using Golang's built in [ParseDuration](https://pkg.go.dev/time#ParseDuration) function.
You can durations to time using the `+` operator.
You can add durations to time using the `+` operator.
## Format: from standard RFC3339 format
Providing a single parameter assumes a standard RFC3339 datetime format. If the target format is not a valid yaml datetime format, the result will be a string tagged node.

61
operators/divide.md Normal file
View File

@ -0,0 +1,61 @@
# Divide
Divide behaves differently according to the type of the LHS:
* strings: split by the divider
* number: arithmetic division
## String split
Given a sample.yml file of:
```yaml
a: cat_meow
b: _
```
then
```bash
yq '.c = .a / .b' sample.yml
```
will output
```yaml
a: cat_meow
b: _
c:
- cat
- meow
```
## Number division
The result during division is calculated as a float
Given a sample.yml file of:
```yaml
a: 12
b: 2.5
```
then
```bash
yq '.a = .a / .b' sample.yml
```
will output
```yaml
a: 4.8
b: 2.5
```
## Number division by zero
Dividing by zero results in +Inf or -Inf
Given a sample.yml file of:
```yaml
a: 1
b: -1
```
then
```bash
yq '.a = .a / 0 | .b = .b / 0' sample.yml
```
will output
```yaml
a: !!float +Inf
b: !!float -Inf
```

View File

@ -309,7 +309,7 @@ cat,"thing1,thing2",true,3.40
dog,thing3,false,12
```
## Encode array of array scalars as tsv string
## Encode array of arrays as tsv string
Scalars are strings, numbers and booleans.
Given a sample.yml file of:

View File

@ -67,7 +67,7 @@ a: 1
b: 2
```
## from_entries with numeric key indexes
## from_entries with numeric key indices
from_entries always creates a map, even for numeric keys
Given a sample.yml file of:

View File

@ -2,9 +2,9 @@
Use `eval` to dynamically process an expression - for instance from an environment variable.
`eval` takes a single argument, and evaluates that as a `yq` expression. Any valid expression can be used, beit a path `.a.b.c | select(. == "cat")`, or an update `.a.b.c = "gogo"`.
`eval` takes a single argument, and evaluates that as a `yq` expression. Any valid expression can be used, be it a path `.a.b.c | select(. == "cat")`, or an update `.a.b.c = "gogo"`.
Tip: This can be useful way parameterise complex scripts.
Tip: This can be a useful way to parameterise complex scripts.
## Dynamically evaluate a path
Given a sample.yml file of:

42
operators/filter.md Normal file
View File

@ -0,0 +1,42 @@
# Filter
Filters an array (or map values) by the expression given. Equivalent to doing `map(select(exp))`.
## Filter array
Given a sample.yml file of:
```yaml
- 1
- 2
- 3
```
then
```bash
yq 'filter(. < 3)' sample.yml
```
will output
```yaml
- 1
- 2
```
## Filter map values
Given a sample.yml file of:
```yaml
c:
things: cool
frog: yes
d:
things: hot
frog: false
```
then
```bash
yq 'filter(.things == "cool")' sample.yml
```
will output
```yaml
- things: cool
frog: yes
```

View File

@ -1,6 +1,6 @@
# Has
This is operation that returns true if the key exists in a map (or index in an array), false otherwise.
This operation returns true if the key exists in a map (or index in an array), false otherwise.
## Has map key
Given a sample.yml file of:

View File

@ -29,7 +29,7 @@ b:
```
then
```bash
yq '.b | key| line' sample.yml
yq '.b | key | line' sample.yml
```
will output
```yaml

75
operators/modulo.md Normal file
View File

@ -0,0 +1,75 @@
# Modulo
Arithmetic modulo operator, returns the remainder from dividing two numbers.
## Number modulo - int
If the lhs and rhs are ints then the expression will be calculated with ints.
Given a sample.yml file of:
```yaml
a: 13
b: 2
```
then
```bash
yq '.a = .a % .b' sample.yml
```
will output
```yaml
a: 1
b: 2
```
## Number modulo - float
If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
```yaml
a: 12
b: 2.5
```
then
```bash
yq '.a = .a % .b' sample.yml
```
will output
```yaml
a: !!float 2
b: 2.5
```
## Number modulo - int by zero
If the lhs is an int and rhs is a 0 the result is an error.
Given a sample.yml file of:
```yaml
a: 1
b: 0
```
then
```bash
yq '.a = .a % .b' sample.yml
```
will output
```bash
Error: cannot modulo by 0
```
## Number modulo - float by zero
If the lhs is a float and rhs is a 0 the result is NaN.
Given a sample.yml file of:
```yaml
a: 1.1
b: 0
```
then
```bash
yq '.a = .a % .b' sample.yml
```
will output
```yaml
a: !!float NaN
b: 0
```

View File

@ -250,7 +250,7 @@ thing:
```
## Merge, deeply merging arrays
Merging arrays deeply means arrays are merge like objects, with indexes as their key. In this case, we merge the first item in the array, and do nothing with the second.
Merging arrays deeply means arrays are merged like objects, with indices as their key. In this case, we merge the first item in the array and do nothing with the second.
Given a sample.yml file of:
```yaml
@ -477,7 +477,7 @@ b: !goat
```
## Custom types: clobber tags
Use the `c` option to clobber custom tags. Note that the second tag is now used
Use the `c` option to clobber custom tags. Note that the second tag is now used.
Given a sample.yml file of:
```yaml

View File

@ -123,7 +123,7 @@ a:
```
## Set path to prune deep paths
Like pick but recursive. This uses `ireduce` to deeply set the selected paths into an empty object,
Like pick but recursive. This uses `ireduce` to deeply set the selected paths into an empty object.
Given a sample.yml file of:
```yaml

View File

@ -27,7 +27,7 @@ myMap:
```
## Pick indices from array
Note that the order of the indexes matches the pick order and non existent indexes are skipped.
Note that the order of the indices matches the pick order and non existent indices are skipped.
Given a sample.yml file of:
```yaml

View File

@ -1,6 +1,6 @@
# Recursive Descent (Glob)
This operator recursively matches (or globs) all children nodes given of a particular element, including that node itself. This is most often used to apply a filter recursively against all matches. It can be used in either the
This operator recursively matches (or globs) all children nodes given of a particular element, including that node itself. This is most often used to apply a filter recursively against all matches.
## match values form `..`
This will, like the `jq` equivalent, recursively match all _value_ nodes. Use it to find/manipulate particular values.
@ -12,7 +12,7 @@ yq '.. style= "flow"' file.yaml
```
## match values and map keys form `...`
The also includes map keys in the results set. This is particularly useful in YAML as unlike JSON, map keys can have their own styling, tags and use anchors and aliases.
The also includes map keys in the results set. This is particularly useful in YAML as unlike JSON, map keys can have their own styling and tags and also use anchors and aliases.
For instance to set the `style` of all nodes in a yaml doc, including the map keys:

View File

@ -62,7 +62,7 @@ going
```
## Select elements from array with regular expression
See more regular expression examples under the `string` operator docs.
See more regular expression examples under the [`string` operator docs](https://mikefarah.gitbook.io/yq/operators/string-operators).
Given a sample.yml file of:
```yaml

View File

@ -10,7 +10,7 @@ yq -i -P 'sort_keys(..)' file2.yml
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 your 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`.
See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-keys) for an example.

View File

@ -1,7 +1,7 @@
# String Operators
## RegEx
This uses golangs native regex functions under the hood - See their [docs](https://github.com/google/re2/wiki/Syntax) for the supported syntax.
This uses Golang's native regex functions under the hood - See their [docs](https://github.com/google/re2/wiki/Syntax) for the supported syntax.
Case insensitive tip: prefix the regex with `(?i)` - e.g. `test("(?i)cats)"`.
@ -15,7 +15,7 @@ Capture returns named RegEx capture groups in a map. Can be more convenient than
Returns true if the string matches the RegEx, false otherwise.
## sub(regEx, replacement)
Substitutes matched substrings. The first parameter is the regEx to match substrings within the original string. The second is a what to replace those matches with. This can refer to capture groups from the first RegEx.
Substitutes matched substrings. The first parameter is the regEx to match substrings within the original string. The second parameter specifies what to replace those matches with. This can refer to capture groups from the first RegEx.
## String blocks, bash and newlines
Bash is notorious for chomping on precious trailing newline characters, making it tricky to set strings with newlines properly. In particular, the `$( exp )` _will trim trailing newlines_.
@ -27,7 +27,7 @@ a: |
cat
```
Using `$( exp )` wont work, as it will trim the trailing new line.
Using `$( exp )` wont work, as it will trim the trailing newline.
```
m=$(echo "cat\n") yq -n '.a = strenv(m)'
@ -49,7 +49,7 @@ a: |
cat
```
Similarly, if you're trying to set the content from a file, and want a trailing new line:
Similarly, if you're trying to set the content from a file, and want a trailing newline:
```
IFS= read -rd '' output < <(cat my_file)
@ -298,7 +298,7 @@ false
```
## Substitute / Replace string
This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)
This uses Golang's regex, described [here](https://github.com/google/re2/wiki/Syntax).
Note the use of `|=` to run in context of the current string value.
Given a sample.yml file of:
@ -315,7 +315,7 @@ a: cats are great
```
## Substitute / Replace string with regex
This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)
This uses Golang's regex, described [here](https://github.com/google/re2/wiki/Syntax).
Note the use of `|=` to run in context of the current string value.
Given a sample.yml file of:

View File

@ -1,6 +1,6 @@
# Subtract
You can use subtract to subtract numbers, as well as removing elements from an array.
You can use subtract to subtract numbers as well as remove elements from an array.
## Array subtraction
Running
@ -59,24 +59,6 @@ a: -1.5
b: 4.5
```
## Number subtraction - float
If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
```yaml
a: 3
b: 4.5
```
then
```bash
yq '.a = .a - .b' sample.yml
```
will output
```yaml
a: -1.5
b: 4.5
```
## Number subtraction - int
If both the lhs and rhs are ints then the expression will be calculated with ints.

View File

@ -1,6 +1,6 @@
# Traverse (Read)
This is the simplest (and perhaps most used) operator, it is used to navigate deeply into yaml structures.
This is the simplest (and perhaps most used) operator. It is used to navigate deeply into yaml structures.
## Simple map navigation
Given a sample.yml file of:
@ -51,7 +51,7 @@ will output
```
## Special characters
Use quotes with brackets around path elements with special characters
Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
```yaml
@ -83,7 +83,7 @@ apple
```
## Keys with spaces
Use quotes with brackets around path elements with special characters
Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
```yaml