Updating docs for v4.19.1 release

This commit is contained in:
Mike Farah 2022-02-06 14:41:27 +11:00
parent dd22aada6d
commit 1b494f46bf
46 changed files with 463 additions and 111 deletions

View File

@ -9,28 +9,11 @@ Add behaves differently according to the type of the LHS:
Use `+=` as a relative append assign for things like increment. Note that `.a += .x` is equivalent to running `.a = .a + .x`. Use `+=` as a relative append assign for things like increment. Note that `.a += .x` is equivalent to running `.a = .a + .x`.
## Concatenate and assign arrays {% hint style="warning" %}
Given a sample.yml file of: Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
```yaml
a: `yq e <exp> <file>`
val: thing {% endhint %}
b:
- cat
- dog
```
then
```bash
yq '.a.b += ["cow"]' sample.yml
```
will output
```yaml
a:
val: thing
b:
- cat
- dog
- cow
```
## Concatenate arrays ## Concatenate arrays
Given a sample.yml file of: Given a sample.yml file of:
@ -54,6 +37,28 @@ will output
- 4 - 4
``` ```
## Concatenate to existing array
Note that the styling of `a` is kept.
Given a sample.yml file of:
```yaml
a: [1,2]
b:
- 3
- 4
```
then
```bash
yq '.a += .b' sample.yml
```
will output
```yaml
a: [1, 2, 3, 4]
b:
- 3
- 4
```
## Concatenate null to array ## Concatenate null to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
@ -71,6 +76,22 @@ will output
- 2 - 2
``` ```
## Append to existing array
Note that the styling is copied from existing array elements
Given a sample.yml file of:
```yaml
a: ['dog']
```
then
```bash
yq '.a += "cat"' sample.yml
```
will output
```yaml
a: ['dog', 'cat']
```
## Add new object to array ## Add new object to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
@ -87,76 +108,6 @@ will output
- cat: meow - cat: meow
``` ```
## Add string to array
Given a sample.yml file of:
```yaml
a:
- 1
- 2
```
then
```bash
yq '.a + "hello"' sample.yml
```
will output
```yaml
- 1
- 2
- hello
```
## Append to array
Given a sample.yml file of:
```yaml
a:
- 1
- 2
b:
- 3
- 4
```
then
```bash
yq '.a = .a + .b' sample.yml
```
will output
```yaml
a:
- 1
- 2
- 3
- 4
b:
- 3
- 4
```
## Append another array using +=
Given a sample.yml file of:
```yaml
a:
- 1
- 2
b:
- 3
- 4
```
then
```bash
yq '.a += .b' sample.yml
```
will output
```yaml
a:
- 1
- 2
- 3
- 4
b:
- 3
- 4
```
## Relative append ## Relative append
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
@ -195,7 +146,7 @@ b: meow
``` ```
then then
```bash ```bash
yq '.a = .a + .b' sample.yml yq '.a += .b' sample.yml
``` ```
will output will output
```yaml ```yaml

View File

@ -2,6 +2,12 @@
This operator is used to provide alternative (or default) values when a particular expression is either null or false. This operator is used to provide alternative (or default) values when a particular expression is either null or false.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## LHS is defined ## LHS is defined
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -5,6 +5,12 @@ Use the `alias` and `anchor` operators to read and write yaml aliases and anchor
`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag. `yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Merge one map ## Merge one map
see https://yaml.org/type/merge.html see https://yaml.org/type/merge.html

View File

@ -7,6 +7,12 @@ Which will assign the LHS node values to the RHS node values. The RHS expression
### relative form: `|=` ### 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, however, the RHS expression is run against _the LHS nodes_. This is useful for updating values based on old values, e.g. increment.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Create yaml file ## Create yaml file
Running Running
```bash ```bash

View File

@ -10,6 +10,12 @@ The `or` and `and` operators take two parameters and return a boolean result.
These are most commonly used with the `select` operator to filter particular nodes. These are most commonly used with the `select` operator to filter particular nodes.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## `or` example ## `or` example
Running Running
```bash ```bash

View File

@ -3,6 +3,12 @@
This creates an array using the expression between the square brackets. This creates an array using the expression between the square brackets.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Collect empty ## Collect empty
Running Running
```bash ```bash

View File

@ -10,6 +10,12 @@ This will assign the LHS nodes comments to the expression on the RHS. The RHS is
### relative form: `|=` ### 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. 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.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Set line comment ## Set line comment
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
This returns `true` if the context contains the passed in parameter, and false otherwise. This returns `true` if the context contains the passed in parameter, and false otherwise.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Array contains array ## Array contains array
Array is equal or subset of Array is equal or subset of

View File

@ -2,6 +2,12 @@
This is used to construct objects (or maps). This can be used against existing yaml, or to create fresh yaml documents. This is used to construct objects (or maps). This can be used against existing yaml, or to create fresh yaml documents.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Collect empty object ## Collect empty object
Running Running
```bash ```bash

View File

@ -2,6 +2,12 @@
Deletes matching entries in maps or arrays. Deletes matching entries in maps or arrays.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Delete entry in map ## Delete entry in map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
Use the `documentIndex` operator (or the `di` shorthand) to select nodes of a particular document. Use the `documentIndex` operator (or the `di` shorthand) to select nodes of a particular document.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Retrieve a document index ## Retrieve a document index
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -22,6 +22,12 @@ CSV and TSV format both accept either a single array or scalars (representing a
XML uses the `--xml-attribute-prefix` and `xml-content-name` flags to identify attributes and content fields. XML uses the `--xml-attribute-prefix` and `xml-content-name` flags to identify attributes and content fields.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Encode value as json string ## Encode value as json string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
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.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## to_entries Map ## to_entries Map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -17,6 +17,12 @@ yq '(.. | select(tag == "!!str")) |= envsubst' file.yaml
``` ```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Read string environment variable ## Read string environment variable
Running Running
```bash ```bash
@ -77,6 +83,28 @@ will output
a: "12" a: "12"
``` ```
## Dynamically update a path from an environment variable
The env variable can be any valid yq expression.
Given a sample.yml file of:
```yaml
a:
b:
- name: dog
- name: cat
```
then
```bash
pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueEnv)' sample.yml
```
will output
```yaml
a:
b:
- name: moo
- name: cat
```
## Dynamic key lookup with environment variable ## Dynamic key lookup with environment variable
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -12,6 +12,12 @@ It is most often used with the select operator to find particular nodes:
select(.a == .b) select(.a == .b)
``` ```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Match string ## Match string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

54
operators/eval.md Normal file
View File

@ -0,0 +1,54 @@
# Eval
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"`.
Tip: This can be useful way parameterise complex scripts.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Dynamically evaluate a path
Given a sample.yml file of:
```yaml
pathExp: .a.b[] | select(.name == "cat")
a:
b:
- name: dog
- name: cat
```
then
```bash
yq 'eval(.pathExp)' sample.yml
```
will output
```yaml
name: cat
```
## Dynamically update a path from an environment variable
The env variable can be any valid yq expression.
Given a sample.yml file of:
```yaml
a:
b:
- name: dog
- name: cat
```
then
```bash
pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueEnv)' sample.yml
```
will output
```yaml
a:
b:
- name: moo
- name: cat
```

View File

@ -10,6 +10,12 @@ Note the use of eval-all to ensure all documents are loaded into memory.
yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
``` ```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Get filename ## Get filename
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -1,6 +1,12 @@
# Flatten # Flatten
This recursively flattens arrays. This recursively flattens arrays.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Flatten ## Flatten
Recursively flattens all arrays Recursively flattens all arrays

View File

@ -2,6 +2,12 @@
This is used to group items in an array by an expression. This is used to group items in an array by an expression.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Group by field ## Group by field
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
This is operation that returns true if the key exists in a map (or index in an array), false otherwise. This is operation that returns true if the key exists in a map (or index in an array), false otherwise.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Has map key ## Has map key
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
Use the `keys` operator to return map keys or array indices. Use the `keys` operator to return map keys or array indices.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Map keys ## Map keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
Returns the lengths of the nodes. Length is defined according to the type of the node. Returns the lengths of the nodes. Length is defined according to the type of the node.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## String length ## String length
returns length of string returns length of string

View File

@ -13,6 +13,12 @@ a: apple is included
b: cool b: cool
``` ```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Simple example ## Simple example
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
Maps values of an array. Use `map_values` to map values of an object. Maps values of an array. Use `map_values` to map values of an object.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Map array ## Map array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -22,6 +22,12 @@ Note the use of `eval-all` to ensure all documents are loaded into memory.
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.yaml yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.yaml
``` ```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Multiply integers ## Multiply integers
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
@ -262,18 +268,23 @@ will output
``` ```
## Merge arrays of objects together, matching on a key ## Merge arrays of objects together, matching on a key
This is a fairly complex expression - you can use it as is by providing the environment variables as seen in the example below.
It merges in the array provided in the second file into the first - matching on equal keys.
Explanation:
The approach, at a high level, is to reduce into a merged map (keyed by the unique key) The approach, at a high level, is to reduce into a merged map (keyed by the unique key)
and then convert that back into an array. and then convert that back into an array.
First the expression will create a map from the arrays keyed by '.a', the unique field we want to merge by. First the expression will create a map from the arrays keyed by the idPath, the unique field we want to merge by.
The reduce operator is merging '({}; . * $item )', so array elements with the matching key will be merged together. The reduce operator is merging '({}; . * $item )', so array elements with the matching key will be merged together.
Next, we convert the map back to an array, using reduce again, concatenating all the map values together. Next, we convert the map back to an array, using reduce again, concatenating all the map values together.
Finally, we set the result of the merged array back into the first doc. Finally, we set the result of the merged array back into the first doc.
To use this, you will need to update '.myArray' in the expression to your array (e.g. .my.array), and '.a' to be the key field of your array (e.g. '.name')
Thanks Kev from [stackoverflow](https://stackoverflow.com/a/70109529/1168223) Thanks Kev from [stackoverflow](https://stackoverflow.com/a/70109529/1168223)
@ -290,7 +301,7 @@ something: else
``` ```
And another sample another.yml file of: And another sample another.yml file of:
```yaml ```yaml
myArray: newArray:
- a: banana - a: banana
c: bananaC c: bananaC
- a: apple - a: apple
@ -300,12 +311,12 @@ myArray:
``` ```
then then
```bash ```bash
yq eval-all ' idPath=".a" originalPath=".myArray" otherPath=".newArray" yq eval-all '
( (
((.myArray[] | {.a: .}) as $item ireduce ({}; . * $item )) as $uniqueMap (( (eval(strenv(originalPath)) + eval(strenv(otherPath))) | .[] | {(eval(strenv(idPath))): .}) as $item ireduce ({}; . * $item )) as $uniqueMap
| ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value) | ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value)
) as $mergedArray ) as $mergedArray
| select(fi == 0) | .myArray = $mergedArray | select(fi == 0) | (eval(strenv(originalPath))) = $mergedArray
' sample.yml another.yml ' sample.yml another.yml
``` ```
will output will output

View File

@ -2,6 +2,12 @@
Parent simply returns the parent nodes of the matching nodes. Parent simply returns the parent nodes of the matching nodes.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Simple example ## Simple example
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -4,6 +4,12 @@ The path operator can be used to get the traversal paths of matching nodes in an
You can get the key/index of matching nodes by using the `path` operator to return the path array then piping that through `.[-1]` to get the last element of that array, the key. You can get the key/index of matching nodes by using the `path` operator to return the path array then piping that through `.[-1]` to get the last element of that array, the key.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Map path ## Map path
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
Pipe the results of an expression into another. Like the bash operator. Pipe the results of an expression into another. Like the bash operator.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Simple Pipe ## Simple Pipe
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -19,6 +19,12 @@ For instance to set the `style` of all nodes in a yaml doc, including the map ke
```bash ```bash
yq '... style= "flow"' file.yaml yq '... style= "flow"' file.yaml
``` ```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Recurse map (values only) ## Recurse map (values only)
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -21,6 +21,12 @@ Reduce syntax in `yq` is a little different from `jq` - as `yq` (currently) isn'
To that end, the reduce operator is called `ireduce` for backwards compatability if a `jq` like prefix version of `reduce` is ever added. To that end, the reduce operator is called `ireduce` for backwards compatability if a `jq` like prefix version of `reduce` is ever added.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Sum numbers ## Sum numbers
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
Select is used to filter arrays and maps by a boolean expression. Select is used to filter arrays and maps by a boolean expression.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Select elements from array using wildcard prefix ## Select elements from array using wildcard prefix
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -12,6 +12,12 @@ 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 your are using merge anchors.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Sort keys of map ## Sort keys of map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -4,6 +4,12 @@ Sorts an array. Use `sort` to sort an array as is, or `sort_by(exp)` to sort by
Note that at this stage, `yq` only sorts scalar fields. Note that at this stage, `yq` only sorts scalar fields.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Sort by string field ## Sort by string field
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
This operator splits all matches into separate documents This operator splits all matches into separate documents
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Split empty ## Split empty
Running Running
```bash ```bash

View File

@ -43,6 +43,12 @@ IFS= read -rd '' output < <(cat my_file)
output=$output ./yq '.data.values = strenv(output)' first.yml output=$output ./yq '.data.values = strenv(output)' first.yml
``` ```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Join strings ## Join strings
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style) The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Update and set style of a particular node (simple) ## Update and set style of a particular node (simple)
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
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 removing elements from an array.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Array subtraction ## Array subtraction
Running Running
```bash ```bash

View File

@ -2,6 +2,12 @@
The tag operator can be used to get or set the tag of nodes (e.g. `!!str`, `!!int`, `!!bool`). The tag operator can be used to get or set the tag of nodes (e.g. `!!str`, `!!int`, `!!bool`).
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Get tag ## Get tag
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
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.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Simple map navigation ## Simple map navigation
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
This operator is used to combine different results together. This operator is used to combine different results together.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Combine scalars ## Combine scalars
Running Running
```bash ```bash

View File

@ -2,6 +2,12 @@
This is used to filter out duplicated items in an array. This is used to filter out duplicated items in an array.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Unique array of scalars (string/numbers) ## Unique array of scalars (string/numbers)
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -4,6 +4,12 @@ Like the `jq` equivalents, variables are sometimes required for the more complex
Note that there is also an additional `ref` operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path. Note that there is also an additional `ref` operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Single value variable ## Single value variable
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -2,6 +2,12 @@
Use the `with` operator to conveniently make multiple updates to a deeply nested path, or to update array elements relatively to each other. The first argument expression sets the root context, and the second expression runs against that root context. Use the `with` operator to conveniently make multiple updates to a deeply nested path, or to update array elements relatively to each other. The first argument expression sets the root context, and the second expression runs against that root context.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Update and style ## Update and style
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -4,6 +4,12 @@ Encode and decode to and from JSON. Note that YAML is a _superset_ of JSON - so
This means you don't need to 'convert' a JSON file to YAML - however if you want idiomatic YAML styling, then you can use the `-P/--prettyPrint` flag, see examples below. This means you don't need to 'convert' a JSON file to YAML - however if you want idiomatic YAML styling, then you can use the `-P/--prettyPrint` flag, see examples below.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Parse json: simple ## Parse json: simple
JSON is a subset of yaml, so all you need to do is prettify the output JSON is a subset of yaml, so all you need to do is prettify the output

View File

@ -1,11 +1,19 @@
# Working with Properties # Properties
## Yaml to Properties Encode to a property file (decode not yet supported). Line comments on value nodes will be copied across.
To convert to property file format, use `--outputformat=props` or `-o=p`. Porting the comments from the `yaml` file to the property file is still in progress, currently line comments on the node values will be copied across.&#x20; By default, empty maps and arrays are not encoded - see below for an example on how to encode a value for these.
Given a sample file of: {% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Encode properties
Note that empty arrays and maps are not encoded by default.
Given a sample.yml file of:
```yaml ```yaml
# block comments don't come through # block comments don't come through
person: # neither do comments on maps person: # neither do comments on maps
@ -13,17 +21,16 @@ person: # neither do comments on maps
pets: pets:
- cat # comments on array values appear - cat # comments on array values appear
food: [pizza] # comments on arrays do not food: [pizza] # comments on arrays do not
``` emptyArray: []
emptyMap: []
```
then then
```bash ```bash
yq -o=p sample.yaml yq -o=props -I=0 '.' sample.yml
```
will output:
``` ```
will output
```properties
# comments on values appear # comments on values appear
person.name = Mike person.name = Mike
@ -32,3 +39,58 @@ person.pets.0 = cat
person.food.0 = pizza person.food.0 = pizza
``` ```
## Encode properties: no comments
Given a sample.yml file of:
```yaml
# block comments don't come through
person: # neither do comments on maps
name: Mike # comments on values appear
pets:
- cat # comments on array values appear
food: [pizza] # comments on arrays do not
emptyArray: []
emptyMap: []
```
then
```bash
yq -o=props -I=0 '... comments = ""' sample.yml
```
will output
```properties
person.name = Mike
person.pets.0 = cat
person.food.0 = pizza
```
## Encode properties: include empty maps and arrays
Use a yq expression to set the empty maps and sequences to your desired value.
Given a sample.yml file of:
```yaml
# block comments don't come through
person: # neither do comments on maps
name: Mike # comments on values appear
pets:
- cat # comments on array values appear
food: [pizza] # comments on arrays do not
emptyArray: []
emptyMap: []
```
then
```bash
yq -o=props -I=0 '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""' sample.yml
```
will output
```properties
# comments on values appear
person.name = Mike
# comments on array values appear
person.pets.0 = cat
person.food.0 = pizza
emptyArray =
emptyMap =
```

View File

@ -6,6 +6,12 @@ Consecutive xml nodes with the same name are assumed to be arrays.
XML content data and attributes are created as fields. This can be controlled by the `'--xml-attribute-prefix` and `--xml-content-name` flags - see below for examples. XML content data and attributes are created as fields. This can be controlled by the `'--xml-attribute-prefix` and `--xml-content-name` flags - see below for examples.
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
## Parse xml: simple ## Parse xml: simple
Notice how all the values are strings, see the next example on how you can fix that. Notice how all the values are strings, see the next example on how you can fix that.