diff --git a/SUMMARY.md b/SUMMARY.md index 88d77525..8ca887f2 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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) diff --git a/operators/anchor-and-alias-operators.md b/operators/anchor-and-alias-operators.md index 8dbfd996..14b0bef2 100644 --- a/operators/anchor-and-alias-operators.md +++ b/operators/anchor-and-alias-operators.md @@ -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 diff --git a/operators/assign-update.md b/operators/assign-update.md index 5885efc9..ccbf5888 100644 --- a/operators/assign-update.md +++ b/operators/assign-update.md @@ -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: diff --git a/operators/comment-operators.md b/operators/comment-operators.md index 3ba9e65c..89af3b1b 100644 --- a/operators/comment-operators.md +++ b/operators/comment-operators.md @@ -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). diff --git a/operators/contains.md b/operators/contains.md index dfdd5a04..157b50d6 100644 --- a/operators/contains.md +++ b/operators/contains.md @@ -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. diff --git a/operators/datetime.md b/operators/datetime.md index bd40053e..685618fc 100644 --- a/operators/datetime.md +++ b/operators/datetime.md @@ -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. diff --git a/operators/divide.md b/operators/divide.md new file mode 100644 index 00000000..6d88949f --- /dev/null +++ b/operators/divide.md @@ -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 +``` + diff --git a/operators/encode-decode.md b/operators/encode-decode.md index b8cc1f75..65f76322 100644 --- a/operators/encode-decode.md +++ b/operators/encode-decode.md @@ -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: diff --git a/operators/entries.md b/operators/entries.md index a466e439..b70b2607 100644 --- a/operators/entries.md +++ b/operators/entries.md @@ -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: diff --git a/operators/eval.md b/operators/eval.md index 1a8a010d..cddd9894 100644 --- a/operators/eval.md +++ b/operators/eval.md @@ -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: diff --git a/operators/filter.md b/operators/filter.md new file mode 100644 index 00000000..7d87cc08 --- /dev/null +++ b/operators/filter.md @@ -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 +``` + diff --git a/operators/has.md b/operators/has.md index 4a5e294a..af66b009 100644 --- a/operators/has.md +++ b/operators/has.md @@ -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: diff --git a/operators/line.md b/operators/line.md index 9fc4cd0c..867c6112 100644 --- a/operators/line.md +++ b/operators/line.md @@ -29,7 +29,7 @@ b: ``` then ```bash -yq '.b | key| line' sample.yml +yq '.b | key | line' sample.yml ``` will output ```yaml diff --git a/operators/modulo.md b/operators/modulo.md new file mode 100644 index 00000000..810f30f3 --- /dev/null +++ b/operators/modulo.md @@ -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 +``` + diff --git a/operators/multiply-merge.md b/operators/multiply-merge.md index 60967413..3d4a67e1 100644 --- a/operators/multiply-merge.md +++ b/operators/multiply-merge.md @@ -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 diff --git a/operators/path.md b/operators/path.md index 3356e867..0d9b73e6 100644 --- a/operators/path.md +++ b/operators/path.md @@ -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 diff --git a/operators/pick.md b/operators/pick.md index 45bccc8b..3b477699 100644 --- a/operators/pick.md +++ b/operators/pick.md @@ -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 diff --git a/operators/recursive-descent-glob.md b/operators/recursive-descent-glob.md index 1bb1656c..cfcb7dae 100644 --- a/operators/recursive-descent-glob.md +++ b/operators/recursive-descent-glob.md @@ -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: diff --git a/operators/select.md b/operators/select.md index 67b91f85..2de0df52 100644 --- a/operators/select.md +++ b/operators/select.md @@ -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 diff --git a/operators/sort-keys.md b/operators/sort-keys.md index 41a4340b..fb8e86ac 100644 --- a/operators/sort-keys.md +++ b/operators/sort-keys.md @@ -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. diff --git a/operators/string-operators.md b/operators/string-operators.md index 585404cf..6aa854bc 100644 --- a/operators/string-operators.md +++ b/operators/string-operators.md @@ -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: diff --git a/operators/subtract.md b/operators/subtract.md index b850cc97..902a3a14 100644 --- a/operators/subtract.md +++ b/operators/subtract.md @@ -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. diff --git a/operators/traverse-read.md b/operators/traverse-read.md index 3a4452e3..fefe24df 100644 --- a/operators/traverse-read.md +++ b/operators/traverse-read.md @@ -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