Simplified yq usage

This commit is contained in:
Mike Farah 2022-01-28 12:45:43 +11:00
parent e84a50db2e
commit 9896245f71
44 changed files with 515 additions and 316 deletions

View File

@ -4,8 +4,10 @@ Add behaves differently according to the type of the LHS:
* arrays: concatenate
* number scalars: arithmetic addition
* string scalars: concatenate
* maps: shallow merge (use the multiply operator (`*`) to deeply merge)
Use `+=` as a relative append assign for things like increment. Note that `.a += .x` is equivalent to running `.a = .a + .x`.
Use `+=` as append assign for things like increment. Note that `.a += .x` is equivalent to running `.a = .a + .x`.
## Concatenate and assign arrays
Given a sample.yml file of:
@ -18,7 +20,7 @@ a:
```
then
```bash
yq eval '.a.b += ["cow"]' sample.yml
yq '.a.b += ["cow"]' sample.yml
```
will output
```yaml
@ -42,7 +44,7 @@ b:
```
then
```bash
yq eval '.a + .b' sample.yml
yq '.a + .b' sample.yml
```
will output
```yaml
@ -61,7 +63,7 @@ a:
```
then
```bash
yq eval '.a + null' sample.yml
yq '.a + null' sample.yml
```
will output
```yaml
@ -77,7 +79,7 @@ a:
```
then
```bash
yq eval '.a + {"cat": "meow"}' sample.yml
yq '.a + {"cat": "meow"}' sample.yml
```
will output
```yaml
@ -94,7 +96,7 @@ a:
```
then
```bash
yq eval '.a + "hello"' sample.yml
yq '.a + "hello"' sample.yml
```
will output
```yaml
@ -115,7 +117,7 @@ b:
```
then
```bash
yq eval '.a = .a + .b' sample.yml
yq '.a = .a + .b' sample.yml
```
will output
```yaml
@ -141,7 +143,7 @@ b:
```
then
```bash
yq eval '.a += .b' sample.yml
yq '.a += .b' sample.yml
```
will output
```yaml
@ -169,7 +171,7 @@ a:
```
then
```bash
yq eval '.a[].b += ["mouse"]' sample.yml
yq '.a[].b += ["mouse"]' sample.yml
```
will output
```yaml
@ -193,7 +195,7 @@ b: meow
```
then
```bash
yq eval '.a = .a + .b' sample.yml
yq '.a = .a + .b' sample.yml
```
will output
```yaml
@ -211,7 +213,7 @@ b: 4.9
```
then
```bash
yq eval '.a = .a + .b' sample.yml
yq '.a = .a + .b' sample.yml
```
will output
```yaml
@ -229,7 +231,7 @@ b: 4
```
then
```bash
yq eval '.a = .a + .b' sample.yml
yq '.a = .a + .b' sample.yml
```
will output
```yaml
@ -245,7 +247,7 @@ b: 5
```
then
```bash
yq eval '.[] += 1' sample.yml
yq '.[] += 1' sample.yml
```
will output
```yaml
@ -258,10 +260,81 @@ Adding to null simply returns the rhs
Running
```bash
yq eval --null-input 'null + "cat"'
yq --null-input 'null + "cat"'
```
will output
```yaml
cat
```
## Add maps to shallow merge
Adding objects together shallow merges them. Use `*` to deeply merge.
Given a sample.yml file of:
```yaml
a:
thing:
name: Astuff
value: x
a1: cool
b:
thing:
name: Bstuff
legs: 3
b1: neat
```
then
```bash
yq '.a += .b' sample.yml
```
will output
```yaml
a:
thing:
name: Bstuff
legs: 3
a1: cool
b1: neat
b:
thing:
name: Bstuff
legs: 3
b1: neat
```
## Custom types: that are really strings
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
```yaml
a: !horse cat
b: !goat _meow
```
then
```bash
yq '.a += .b' sample.yml
```
will output
```yaml
a: !horse cat_meow
b: !goat _meow
```
## Custom types: that are really numbers
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
```yaml
a: !horse 1.2
b: !goat 2.3
```
then
```bash
yq '.a += .b' sample.yml
```
will output
```yaml
a: !horse 3.5
b: !goat 2.3
```

View File

@ -9,7 +9,7 @@ a: bridge
```
then
```bash
yq eval '.a // "hello"' sample.yml
yq '.a // "hello"' sample.yml
```
will output
```yaml
@ -23,7 +23,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.a // "hello"' sample.yml
yq '.a // "hello"' sample.yml
```
will output
```yaml
@ -37,7 +37,7 @@ a: ~
```
then
```bash
yq eval '.a // "hello"' sample.yml
yq '.a // "hello"' sample.yml
```
will output
```yaml
@ -51,7 +51,7 @@ a: false
```
then
```bash
yq eval '.a // "hello"' sample.yml
yq '.a // "hello"' sample.yml
```
will output
```yaml
@ -66,7 +66,7 @@ b: cat
```
then
```bash
yq eval '.a // .b' sample.yml
yq '.a // .b' sample.yml
```
will output
```yaml

View File

@ -25,7 +25,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[4] | explode(.)' sample.yml
yq '.[4] | explode(.)' sample.yml
```
will output
```yaml
@ -55,7 +55,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[4] | explode(.)' sample.yml
yq '.[4] | explode(.)' sample.yml
```
will output
```yaml
@ -87,7 +87,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[4] | explode(.)' sample.yml
yq '.[4] | explode(.)' sample.yml
```
will output
```yaml
@ -103,7 +103,7 @@ a: &billyBob cat
```
then
```bash
yq eval '.a | anchor' sample.yml
yq '.a | anchor' sample.yml
```
will output
```yaml
@ -117,7 +117,7 @@ a: cat
```
then
```bash
yq eval '.a anchor = "foobar"' sample.yml
yq '.a anchor = "foobar"' sample.yml
```
will output
```yaml
@ -132,7 +132,7 @@ a:
```
then
```bash
yq eval '.a anchor |= .b' sample.yml
yq '.a anchor |= .b' sample.yml
```
will output
```yaml
@ -148,7 +148,7 @@ a: *billyBob
```
then
```bash
yq eval '.a | alias' sample.yml
yq '.a | alias' sample.yml
```
will output
```yaml
@ -163,7 +163,7 @@ a: cat
```
then
```bash
yq eval '.a alias = "meow"' sample.yml
yq '.a alias = "meow"' sample.yml
```
will output
```yaml
@ -179,7 +179,7 @@ a: cat
```
then
```bash
yq eval '.a alias = ""' sample.yml
yq '.a alias = ""' sample.yml
```
will output
```yaml
@ -196,7 +196,7 @@ a:
```
then
```bash
yq eval '.a alias |= .f' sample.yml
yq '.a alias |= .f' sample.yml
```
will output
```yaml
@ -213,7 +213,7 @@ f:
```
then
```bash
yq eval 'explode(.f)' sample.yml
yq 'explode(.f)' sample.yml
```
will output
```yaml
@ -229,7 +229,7 @@ a: mike
```
then
```bash
yq eval 'explode(.a)' sample.yml
yq 'explode(.a)' sample.yml
```
will output
```yaml
@ -245,7 +245,7 @@ f:
```
then
```bash
yq eval 'explode(.f)' sample.yml
yq 'explode(.f)' sample.yml
```
will output
```yaml
@ -278,7 +278,7 @@ foobar:
```
then
```bash
yq eval 'explode(.)' sample.yml
yq 'explode(.)' sample.yml
```
will output
```yaml
@ -317,7 +317,7 @@ thingTwo:
```
then
```bash
yq eval '.thingOne |= explode(.) * {"value": false}' sample.yml
yq '.thingOne |= explode(.) * {"value": false}' sample.yml
```
will output
```yaml

View File

@ -10,7 +10,7 @@ This will do a similar thing to the plain form, however, the RHS expression is r
## Create yaml file
Running
```bash
yq eval --null-input '.a.b = "cat" | .x = "frog"'
yq --null-input '.a.b = "cat" | .x = "frog"'
```
will output
```yaml
@ -28,7 +28,7 @@ a:
```
then
```bash
yq eval '.a |= .b' sample.yml
yq '.a |= .b' sample.yml
```
will output
```yaml
@ -45,7 +45,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] |= . * 2' sample.yml
yq '.[] |= . * 2' sample.yml
```
will output
```yaml
@ -84,7 +84,7 @@ b: sibling
```
then
```bash
yq eval '.a = .b' sample.yml
yq '.a = .b' sample.yml
```
will output
```yaml
@ -101,7 +101,7 @@ c: fieldC
```
then
```bash
yq eval '(.a, .c) = "potatoe"' sample.yml
yq '(.a, .c) = "potatoe"' sample.yml
```
will output
```yaml
@ -118,7 +118,7 @@ a:
```
then
```bash
yq eval '.a.b = "frog"' sample.yml
yq '.a.b = "frog"' sample.yml
```
will output
```yaml
@ -136,7 +136,7 @@ a:
```
then
```bash
yq eval '.a.b |= "frog"' sample.yml
yq '.a.b |= "frog"' sample.yml
```
will output
```yaml
@ -155,7 +155,7 @@ a:
```
then
```bash
yq eval '(.a[] | select(. == "apple")) = "frog"' sample.yml
yq '(.a[] | select(. == "apple")) = "frog"' sample.yml
```
will output
```yaml
@ -173,7 +173,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '(.[] | select(. == "*andy")) = "bogs"' sample.yml
yq '(.[] | select(. == "*andy")) = "bogs"' sample.yml
```
will output
```yaml
@ -189,7 +189,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.a.b |= "bogs"' sample.yml
yq '.a.b |= "bogs"' sample.yml
```
will output
```yaml
@ -205,7 +205,7 @@ a: &cool cat
```
then
```bash
yq eval '.a = "dog"' sample.yml
yq '.a = "dog"' sample.yml
```
will output
```yaml
@ -219,7 +219,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.a.b.[0] |= "bogs"' sample.yml
yq '.a.b.[0] |= "bogs"' sample.yml
```
will output
```yaml

View File

@ -13,7 +13,7 @@ These are most commonly used with the `select` operator to filter particular nod
## `or` example
Running
```bash
yq eval --null-input 'true or false'
yq --null-input 'true or false'
```
will output
```yaml
@ -23,7 +23,7 @@ true
## `and` example
Running
```bash
yq eval --null-input 'true and false'
yq --null-input 'true and false'
```
will output
```yaml
@ -42,7 +42,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml
yq '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml
```
will output
```yaml
@ -60,7 +60,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'any' sample.yml
yq 'any' sample.yml
```
will output
```yaml
@ -74,7 +74,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'any' sample.yml
yq 'any' sample.yml
```
will output
```yaml
@ -93,7 +93,7 @@ b:
```
then
```bash
yq eval '.[] |= any_c(. == "awesome")' sample.yml
yq '.[] |= any_c(. == "awesome")' sample.yml
```
will output
```yaml
@ -109,7 +109,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'all' sample.yml
yq 'all' sample.yml
```
will output
```yaml
@ -123,7 +123,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'all' sample.yml
yq 'all' sample.yml
```
will output
```yaml
@ -142,7 +142,7 @@ b:
```
then
```bash
yq eval '.[] |= all_c(tag == "!!str")' sample.yml
yq '.[] |= all_c(tag == "!!str")' sample.yml
```
will output
```yaml
@ -153,7 +153,7 @@ b: false
## Not true is false
Running
```bash
yq eval --null-input 'true | not'
yq --null-input 'true | not'
```
will output
```yaml
@ -163,7 +163,7 @@ false
## Not false is true
Running
```bash
yq eval --null-input 'false | not'
yq --null-input 'false | not'
```
will output
```yaml
@ -173,7 +173,7 @@ true
## String values considered to be true
Running
```bash
yq eval --null-input '"cat" | not'
yq --null-input '"cat" | not'
```
will output
```yaml
@ -183,7 +183,7 @@ false
## Empty string value considered to be true
Running
```bash
yq eval --null-input '"" | not'
yq --null-input '"" | not'
```
will output
```yaml
@ -193,7 +193,7 @@ false
## Numbers are considered to be true
Running
```bash
yq eval --null-input '1 | not'
yq --null-input '1 | not'
```
will output
```yaml
@ -203,7 +203,7 @@ false
## Zero is considered to be true
Running
```bash
yq eval --null-input '0 | not'
yq --null-input '0 | not'
```
will output
```yaml
@ -213,7 +213,7 @@ false
## Null is considered to be false
Running
```bash
yq eval --null-input '~ | not'
yq --null-input '~ | not'
```
will output
```yaml

View File

@ -6,7 +6,7 @@ This creates an array using the expression between the square brackets.
## Collect empty
Running
```bash
yq eval --null-input '[]'
yq --null-input '[]'
```
will output
```yaml
@ -16,7 +16,7 @@ will output
## Collect single
Running
```bash
yq eval --null-input '["cat"]'
yq --null-input '["cat"]'
```
will output
```yaml
@ -31,7 +31,7 @@ b: dog
```
then
```bash
yq eval '[.a, .b]' sample.yml
yq '[.a, .b]' sample.yml
```
will output
```yaml

View File

@ -17,7 +17,7 @@ a: cat
```
then
```bash
yq eval '.a lineComment="single"' sample.yml
yq '.a lineComment="single"' sample.yml
```
will output
```yaml
@ -32,7 +32,7 @@ b: dog
```
then
```bash
yq eval '.. lineComment |= .' sample.yml
yq '.. lineComment |= .' sample.yml
```
will output
```yaml
@ -47,7 +47,7 @@ a: cat
```
then
```bash
yq eval '. headComment="single"' sample.yml
yq '. headComment="single"' sample.yml
```
will output
```yaml
@ -63,7 +63,7 @@ a: cat
```
then
```bash
yq eval '. footComment=.a' sample.yml
yq '. footComment=.a' sample.yml
```
will output
```yaml
@ -80,7 +80,7 @@ b: dog # leave this
```
then
```bash
yq eval '.a lineComment=""' sample.yml
yq '.a lineComment=""' sample.yml
```
will output
```yaml
@ -99,7 +99,7 @@ b: # key comment
```
then
```bash
yq eval '... comments=""' sample.yml
yq '... comments=""' sample.yml
```
will output
```yaml
@ -114,7 +114,7 @@ a: cat # meow
```
then
```bash
yq eval '.a | lineComment' sample.yml
yq '.a | lineComment' sample.yml
```
will output
```yaml
@ -132,7 +132,7 @@ a: cat # meow
```
then
```bash
yq eval '. | headComment' sample.yml
yq '. | headComment' sample.yml
```
will output
```yaml
@ -151,7 +151,7 @@ a: cat # meow
```
then
```bash
yq eval 'headComment' sample.yml
yq 'headComment' sample.yml
```
will output
```yaml
@ -171,7 +171,7 @@ a: cat # meow
```
then
```bash
yq eval '. | footComment' sample.yml
yq '. | footComment' sample.yml
```
will output
```yaml

View File

@ -13,7 +13,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'contains(["baz", "bar"])' sample.yml
yq 'contains(["baz", "bar"])' sample.yml
```
will output
```yaml
@ -32,7 +32,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'contains({"bar": [{"barp": 12}]})' sample.yml
yq 'contains({"bar": [{"barp": 12}]})' sample.yml
```
will output
```yaml
@ -51,7 +51,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'contains({"foo": 12, "bar": [{"barp": 15}]})' sample.yml
yq 'contains({"foo": 12, "bar": [{"barp": 15}]})' sample.yml
```
will output
```yaml
@ -65,7 +65,7 @@ foobar
```
then
```bash
yq eval 'contains("bar")' sample.yml
yq 'contains("bar")' sample.yml
```
will output
```yaml
@ -79,7 +79,7 @@ meow
```
then
```bash
yq eval 'contains("meow")' sample.yml
yq 'contains("meow")' sample.yml
```
will output
```yaml

View File

@ -5,7 +5,7 @@ This is used to construct objects (or maps). This can be used against existing y
## Collect empty object
Running
```bash
yq eval --null-input '{}'
yq --null-input '{}'
```
will output
```yaml
@ -19,7 +19,7 @@ name: Mike
```
then
```bash
yq eval '{"wrap": .}' sample.yml
yq '{"wrap": .}' sample.yml
```
will output
```yaml
@ -37,7 +37,7 @@ pets:
```
then
```bash
yq eval '{.name: .pets.[]}' sample.yml
yq '{.name: .pets.[]}' sample.yml
```
will output
```yaml
@ -60,7 +60,7 @@ pets:
```
then
```bash
yq eval '{.name: .pets.[]}' sample.yml
yq '{.name: .pets.[]}' sample.yml
```
will output
```yaml
@ -73,7 +73,7 @@ Rosey: sheep
## Creating yaml from scratch
Running
```bash
yq eval --null-input '{"wrap": "frog"}'
yq --null-input '{"wrap": "frog"}'
```
will output
```yaml

View File

@ -10,7 +10,7 @@ b: dog
```
then
```bash
yq eval 'del(.b)' sample.yml
yq 'del(.b)' sample.yml
```
will output
```yaml
@ -26,7 +26,7 @@ a:
```
then
```bash
yq eval 'del(.a.a1)' sample.yml
yq 'del(.a.a1)' sample.yml
```
will output
```yaml
@ -43,7 +43,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'del(.[1])' sample.yml
yq 'del(.[1])' sample.yml
```
will output
```yaml
@ -59,7 +59,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'del(.[0].a)' sample.yml
yq 'del(.[0].a)' sample.yml
```
will output
```yaml
@ -74,7 +74,7 @@ b: dog
```
then
```bash
yq eval 'del(.c)' sample.yml
yq 'del(.c)' sample.yml
```
will output
```yaml
@ -91,7 +91,7 @@ c: bat
```
then
```bash
yq eval 'del( .[] | select(. == "*at") )' sample.yml
yq 'del( .[] | select(. == "*at") )' sample.yml
```
will output
```yaml
@ -109,7 +109,7 @@ a:
```
then
```bash
yq eval 'del(.. | select(has("name")).name)' sample.yml
yq 'del(.. | select(has("name")).name)' sample.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ a: frog
```
then
```bash
yq eval '.a | documentIndex' sample.yml
yq '.a | documentIndex' sample.yml
```
will output
```yaml
@ -29,7 +29,7 @@ a: frog
```
then
```bash
yq eval '.a | di' sample.yml
yq '.a | di' sample.yml
```
will output
```yaml
@ -47,7 +47,7 @@ a: frog
```
then
```bash
yq eval 'select(documentIndex == 1)' sample.yml
yq 'select(documentIndex == 1)' sample.yml
```
will output
```yaml
@ -63,7 +63,7 @@ a: frog
```
then
```bash
yq eval 'select(di == 1)' sample.yml
yq 'select(di == 1)' sample.yml
```
will output
```yaml
@ -79,7 +79,7 @@ a: frog
```
then
```bash
yq eval '.a | ({"match": ., "doc": documentIndex})' sample.yml
yq '.a | ({"match": ., "doc": documentIndex})' sample.yml
```
will output
```yaml

View File

@ -30,7 +30,7 @@ a:
```
then
```bash
yq eval '.b = (.a | to_json)' sample.yml
yq '.b = (.a | to_json)' sample.yml
```
will output
```yaml
@ -52,7 +52,7 @@ a:
```
then
```bash
yq eval '.b = (.a | to_json(0))' sample.yml
yq '.b = (.a | to_json(0))' sample.yml
```
will output
```yaml
@ -71,7 +71,7 @@ a:
```
then
```bash
yq eval '.b = (.a | @json)' sample.yml
yq '.b = (.a | @json)' sample.yml
```
will output
```yaml
@ -89,7 +89,7 @@ a: '{"cool":"thing"}'
```
then
```bash
yq eval '.a | from_json | ... style=""' sample.yml
yq '.a | from_json | ... style=""' sample.yml
```
will output
```yaml
@ -104,7 +104,7 @@ a:
```
then
```bash
yq eval '.b = (.a | @props)' sample.yml
yq '.b = (.a | @props)' sample.yml
```
will output
```yaml
@ -125,7 +125,7 @@ a:
```
then
```bash
yq eval '.b = (.a | to_yaml)' sample.yml
yq '.b = (.a | to_yaml)' sample.yml
```
will output
```yaml
@ -148,7 +148,7 @@ a:
```
then
```bash
yq eval '.b = (.a | to_yaml(8))' sample.yml
yq '.b = (.a | to_yaml(8))' sample.yml
```
will output
```yaml
@ -167,7 +167,7 @@ a: 'foo: bar'
```
then
```bash
yq eval '.b = (.a | from_yaml)' sample.yml
yq '.b = (.a | from_yaml)' sample.yml
```
will output
```yaml
@ -186,7 +186,7 @@ a: |
```
then
```bash
yq eval '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
```
will output
```yaml
@ -202,7 +202,7 @@ a: 'foo: bar'
```
then
```bash
yq eval '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
```
will output
```yaml
@ -221,7 +221,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '@csv' sample.yml
yq '@csv' sample.yml
```
will output
```yaml
@ -242,7 +242,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '@csv' sample.yml
yq '@csv' sample.yml
```
will output
```yaml
@ -266,7 +266,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '@tsv' sample.yml
yq '@tsv' sample.yml
```
will output
```yaml
@ -284,7 +284,7 @@ a:
```
then
```bash
yq eval '.a | to_xml' sample.yml
yq '.a | to_xml' sample.yml
```
will output
```yaml
@ -304,7 +304,7 @@ a:
```
then
```bash
yq eval '.a | @xml' sample.yml
yq '.a | @xml' sample.yml
```
will output
```yaml
@ -322,7 +322,7 @@ a:
```
then
```bash
yq eval '{"cat": .a | to_xml(1)}' sample.yml
yq '{"cat": .a | to_xml(1)}' sample.yml
```
will output
```yaml
@ -339,7 +339,7 @@ a: <foo>bar</foo>
```
then
```bash
yq eval '.b = (.a | from_xml)' sample.yml
yq '.b = (.a | from_xml)' sample.yml
```
will output
```yaml

View File

@ -10,7 +10,7 @@ b: 2
```
then
```bash
yq eval 'to_entries' sample.yml
yq 'to_entries' sample.yml
```
will output
```yaml
@ -28,7 +28,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'to_entries' sample.yml
yq 'to_entries' sample.yml
```
will output
```yaml
@ -45,7 +45,7 @@ null
```
then
```bash
yq eval 'to_entries' sample.yml
yq 'to_entries' sample.yml
```
will output
```yaml
@ -59,7 +59,7 @@ b: 2
```
then
```bash
yq eval 'to_entries | from_entries' sample.yml
yq 'to_entries | from_entries' sample.yml
```
will output
```yaml
@ -77,7 +77,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'to_entries | from_entries' sample.yml
yq 'to_entries | from_entries' sample.yml
```
will output
```yaml
@ -93,7 +93,7 @@ b: 2
```
then
```bash
yq eval 'with_entries(.key |= "KEY_" + .)' sample.yml
yq 'with_entries(.key |= "KEY_" + .)' sample.yml
```
will output
```yaml
@ -111,7 +111,7 @@ c:
```
then
```bash
yq eval 'with_entries(select(.value | has("b")))' sample.yml
yq 'with_entries(select(.value | has("b")))' sample.yml
```
will output
```yaml

View File

@ -1,11 +1,26 @@
# Env Variable Operators
This operator is used to handle environment variables usage in path expressions. While environment variables can, of course, be passed in via your CLI with string interpolation, this often comes with complex quote escaping and can be tricky to write and read. Note that there are two forms, `env` which will parse the environment variable as a yaml (be it a map, array, string, number of boolean) and `strenv` which will always parse the argument as a string.
These operators are used to handle environment variables usage in expressions and documents. While environment variables can, of course, be passed in via your CLI with string interpolation, this often comes with complex quote escaping and can be tricky to write and read.
There are three operators:
- `env` which takes a single environment variable name and parse the variable as a yaml node (be it a map, array, string, number of boolean)
- `strenv` which also takes a single environment variable name, and always parses the variable as a string.
- `envsubst` which you pipe strings into and it interpolates environment variables in strings using [envsubst](https://github.com/a8m/envsubst).
## Tip
To replace environment variables across all values in a document, `envsubst` can be used with the recursive descent operator
as follows:
```bash
yq '(.. | select(tag == "!!str")) |= envsubst' file.yaml
```
## Read string environment variable
Running
```bash
myenv="cat meow" yq eval --null-input '.a = env(myenv)'
myenv="cat meow" yq --null-input '.a = env(myenv)'
```
will output
```yaml
@ -15,7 +30,7 @@ a: cat meow
## Read boolean environment variable
Running
```bash
myenv="true" yq eval --null-input '.a = env(myenv)'
myenv="true" yq --null-input '.a = env(myenv)'
```
will output
```yaml
@ -25,7 +40,7 @@ a: true
## Read numeric environment variable
Running
```bash
myenv="12" yq eval --null-input '.a = env(myenv)'
myenv="12" yq --null-input '.a = env(myenv)'
```
will output
```yaml
@ -35,7 +50,7 @@ a: 12
## Read yaml environment variable
Running
```bash
myenv="{b: fish}" yq eval --null-input '.a = env(myenv)'
myenv="{b: fish}" yq --null-input '.a = env(myenv)'
```
will output
```yaml
@ -45,7 +60,7 @@ a: {b: fish}
## Read boolean environment variable as a string
Running
```bash
myenv="true" yq eval --null-input '.a = strenv(myenv)'
myenv="true" yq --null-input '.a = strenv(myenv)'
```
will output
```yaml
@ -55,7 +70,7 @@ a: "true"
## Read numeric environment variable as a string
Running
```bash
myenv="12" yq eval --null-input '.a = strenv(myenv)'
myenv="12" yq --null-input '.a = strenv(myenv)'
```
will output
```yaml
@ -70,10 +85,54 @@ dog: woof
```
then
```bash
myenv="cat" yq eval '.[env(myenv)]' sample.yml
myenv="cat" yq '.[env(myenv)]' sample.yml
```
will output
```yaml
meow
```
## Replace strings with envsubst
Running
```bash
myenv="cat" yq --null-input '"the ${myenv} meows" | envsubst'
```
will output
```yaml
the cat meows
```
## Replace strings with envsubst, missing variables
Running
```bash
myenv="cat" yq --null-input '"the ${myenvnonexisting} meows" | envsubst'
```
will output
```yaml
the meows
```
## Replace strings with envsubst, missing variables with defaults
Running
```bash
myenv="cat" yq --null-input '"the ${myenvnonexisting-dog} meows" | envsubst'
```
will output
```yaml
the dog meows
```
## Replace string environment variable in document
Given a sample.yml file of:
```yaml
v: ${myenv}
```
then
```bash
myenv="cat meow" yq '.v |= envsubst' sample.yml
```
will output
```yaml
v: cat meow
```

View File

@ -21,7 +21,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | (. == "*at")' sample.yml
yq '.[] | (. == "*at")' sample.yml
```
will output
```yaml
@ -39,7 +39,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | (. != "*at")' sample.yml
yq '.[] | (. != "*at")' sample.yml
```
will output
```yaml
@ -57,7 +57,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | (. == 4)' sample.yml
yq '.[] | (. == 4)' sample.yml
```
will output
```yaml
@ -75,7 +75,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | (. != 4)' sample.yml
yq '.[] | (. != 4)' sample.yml
```
will output
```yaml
@ -87,7 +87,7 @@ true
## Match nulls
Running
```bash
yq eval --null-input 'null == ~'
yq --null-input 'null == ~'
```
will output
```yaml
@ -101,7 +101,7 @@ a: frog
```
then
```bash
yq eval 'select(.b != "thing")' sample.yml
yq 'select(.b != "thing")' sample.yml
```
will output
```yaml
@ -115,7 +115,7 @@ a: frog
```
then
```bash
yq eval 'select(.b == .c)' sample.yml
yq 'select(.b == .c)' sample.yml
```
will output
```yaml

View File

@ -17,7 +17,7 @@ a: cat
```
then
```bash
yq eval 'filename' sample.yml
yq 'filename' sample.yml
```
will output
```yaml
@ -31,7 +31,7 @@ a: cat
```
then
```bash
yq eval 'fileIndex' sample.yml
yq 'fileIndex' sample.yml
```
will output
```yaml
@ -65,7 +65,7 @@ a: cat
```
then
```bash
yq eval 'fi' sample.yml
yq 'fi' sample.yml
```
will output
```yaml

View File

@ -12,7 +12,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'flatten' sample.yml
yq 'flatten' sample.yml
```
will output
```yaml
@ -30,7 +30,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'flatten(1)' sample.yml
yq 'flatten(1)' sample.yml
```
will output
```yaml
@ -46,7 +46,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'flatten' sample.yml
yq 'flatten' sample.yml
```
will output
```yaml
@ -61,7 +61,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'flatten' sample.yml
yq 'flatten' sample.yml
```
will output
```yaml

View File

@ -14,7 +14,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'group_by(.foo)' sample.yml
yq 'group_by(.foo)' sample.yml
```
will output
```yaml
@ -40,7 +40,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'group_by(.foo)' sample.yml
yq 'group_by(.foo)' sample.yml
```
will output
```yaml

View File

@ -12,7 +12,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | has("a")' sample.yml
yq '.[] | has("a")' sample.yml
```
will output
```yaml
@ -36,7 +36,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | select(.a.b | has("c"))' sample.yml
yq '.[] | select(.a.b | has("c"))' sample.yml
```
will output
```yaml
@ -57,7 +57,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | has(1)' sample.yml
yq '.[] | has(1)' sample.yml
```
will output
```yaml

View File

@ -10,7 +10,7 @@ cat: meow
```
then
```bash
yq eval 'keys' sample.yml
yq 'keys' sample.yml
```
will output
```yaml
@ -26,7 +26,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'keys' sample.yml
yq 'keys' sample.yml
```
will output
```yaml
@ -43,7 +43,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[1] | key' sample.yml
yq '.[1] | key' sample.yml
```
will output
```yaml
@ -57,7 +57,7 @@ a: thing
```
then
```bash
yq eval '.a | key' sample.yml
yq '.a | key' sample.yml
```
will output
```yaml
@ -71,7 +71,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'key' sample.yml
yq 'key' sample.yml
```
will output
```yaml
@ -86,7 +86,7 @@ a:
```
then
```bash
yq eval '(.a.x | key) = "meow"' sample.yml
yq '(.a.x | key) = "meow"' sample.yml
```
will output
```yaml
@ -105,7 +105,7 @@ a:
```
then
```bash
yq eval '.a.x | key | headComment' sample.yml
yq '.a.x | key | headComment' sample.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ a: cat
```
then
```bash
yq eval '.a | length' sample.yml
yq '.a | length' sample.yml
```
will output
```yaml
@ -25,7 +25,7 @@ a: null
```
then
```bash
yq eval '.a | length' sample.yml
yq '.a | length' sample.yml
```
will output
```yaml
@ -42,7 +42,7 @@ c: dog
```
then
```bash
yq eval 'length' sample.yml
yq 'length' sample.yml
```
will output
```yaml
@ -61,7 +61,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'length' sample.yml
yq 'length' sample.yml
```
will output
```yaml

View File

@ -20,7 +20,7 @@ myFile: ../../examples/thing.yml
```
then
```bash
yq eval 'load(.myFile)' sample.yml
yq 'load(.myFile)' sample.yml
```
will output
```yaml
@ -38,7 +38,7 @@ something:
```
then
```bash
yq eval '.something |= load("../../examples/" + .file)' sample.yml
yq '.something |= load("../../examples/" + .file)' sample.yml
```
will output
```yaml
@ -60,7 +60,7 @@ over:
```
then
```bash
yq eval '(.. | select(has("file"))) |= load("../../examples/" + .file)' sample.yml
yq '(.. | select(has("file"))) |= load("../../examples/" + .file)' sample.yml
```
will output
```yaml
@ -83,7 +83,7 @@ something:
```
then
```bash
yq eval '.something |= strload("../../examples/" + .file)' sample.yml
yq '.something |= strload("../../examples/" + .file)' sample.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'map(. + 1)' sample.yml
yq 'map(. + 1)' sample.yml
```
will output
```yaml
@ -29,7 +29,7 @@ c: 3
```
then
```bash
yq eval 'map_values(. + 1)' sample.yml
yq 'map_values(. + 1)' sample.yml
```
will output
```yaml

View File

@ -23,13 +23,19 @@ yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.y
```
## Multiply integers
Running
Given a sample.yml file of:
```yaml
a: 3
b: 4
```
then
```bash
yq eval --null-input '3 * 4'
yq '.a *= .b' sample.yml
```
will output
```yaml
12
a: 12
b: 4
```
## Merge objects together, returning merged result only
@ -45,7 +51,7 @@ b:
```
then
```bash
yq eval '.a * .b' sample.yml
yq '.a * .b' sample.yml
```
will output
```yaml
@ -68,7 +74,7 @@ b:
```
then
```bash
yq eval '. * {"a":.b}' sample.yml
yq '. * {"a":.b}' sample.yml
```
will output
```yaml
@ -92,7 +98,7 @@ b:
```
then
```bash
yq eval '. * {"a":.b}' sample.yml
yq '. * {"a":.b}' sample.yml
```
will output
```yaml
@ -115,7 +121,7 @@ b:
```
then
```bash
yq eval '. * {"a":.b}' sample.yml
yq '. * {"a":.b}' sample.yml
```
will output
```yaml
@ -141,7 +147,7 @@ b:
```
then
```bash
yq eval '.a *? .b' sample.yml
yq '.a *? .b' sample.yml
```
will output
```yaml
@ -161,7 +167,7 @@ b:
```
then
```bash
yq eval '.a *n .b' sample.yml
yq '.a *n .b' sample.yml
```
will output
```yaml
@ -188,7 +194,7 @@ b:
```
then
```bash
yq eval '.a *+ .b' sample.yml
yq '.a *+ .b' sample.yml
```
will output
```yaml
@ -218,7 +224,7 @@ b:
```
then
```bash
yq eval '.a *?+ .b' sample.yml
yq '.a *?+ .b' sample.yml
```
will output
```yaml
@ -245,7 +251,7 @@ b:
```
then
```bash
yq eval '.a *d .b' sample.yml
yq '.a *d .b' sample.yml
```
will output
```yaml
@ -325,7 +331,7 @@ b: dog
```
then
```bash
yq eval '. * {"a": {"c": .a}}' sample.yml
yq '. * {"a": {"c": .a}}' sample.yml
```
will output
```yaml
@ -346,7 +352,7 @@ c:
```
then
```bash
yq eval '.c * .b' sample.yml
yq '.c * .b' sample.yml
```
will output
```yaml
@ -366,7 +372,7 @@ c:
```
then
```bash
yq eval '.c * .a' sample.yml
yq '.c * .a' sample.yml
```
will output
```yaml
@ -398,7 +404,7 @@ foobar:
```
then
```bash
yq eval '.foobar * .foobarList' sample.yml
yq '.foobar * .foobarList' sample.yml
```
will output
```yaml
@ -410,3 +416,44 @@ thing: foobar_thing
b: foobarList_b
```
## Custom types: that are really numbers
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
```yaml
a: !horse 2
b: !goat 3
```
then
```bash
yq '.a = .a * .b' sample.yml
```
will output
```yaml
a: !horse 6
b: !goat 3
```
## Custom types: that are really maps
Custom tags will be maintained.
Given a sample.yml file of:
```yaml
a: !horse
cat: meow
b: !goat
dog: woof
```
then
```bash
yq '.a = .a * .b' sample.yml
```
will output
```yaml
a: !horse
cat: meow
dog: woof
b: !goat
dog: woof
```

View File

@ -10,7 +10,7 @@ a:
```
then
```bash
yq eval '.a.nested | parent' sample.yml
yq '.a.nested | parent' sample.yml
```
will output
```yaml
@ -29,7 +29,7 @@ b:
```
then
```bash
yq eval '.. | select(. == "banana") | parent' sample.yml
yq '.. | select(. == "banana") | parent' sample.yml
```
will output
```yaml
@ -44,7 +44,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'parent' sample.yml
yq 'parent' sample.yml
```
will output
```yaml

View File

@ -12,7 +12,7 @@ a:
```
then
```bash
yq eval '.a.b | path' sample.yml
yq '.a.b | path' sample.yml
```
will output
```yaml
@ -28,7 +28,7 @@ a:
```
then
```bash
yq eval '.a.b | path | .[-1]' sample.yml
yq '.a.b | path | .[-1]' sample.yml
```
will output
```yaml
@ -44,7 +44,7 @@ a:
```
then
```bash
yq eval '.a.[] | select(. == "dog") | path' sample.yml
yq '.a.[] | select(. == "dog") | path' sample.yml
```
will output
```yaml
@ -61,7 +61,7 @@ a:
```
then
```bash
yq eval '.a.[] | select(. == "dog") | path | .[-1]' sample.yml
yq '.a.[] | select(. == "dog") | path | .[-1]' sample.yml
```
will output
```yaml
@ -78,7 +78,7 @@ a:
```
then
```bash
yq eval '.a[] | select(. == "*og") | [{"path":path, "value":.}]' sample.yml
yq '.a[] | select(. == "*og") | [{"path":path, "value":.}]' sample.yml
```
will output
```yaml

View File

@ -10,7 +10,7 @@ a:
```
then
```bash
yq eval '.a | .b' sample.yml
yq '.a | .b' sample.yml
```
will output
```yaml
@ -26,7 +26,7 @@ c: same
```
then
```bash
yq eval '.a = "cat" | .b = "dog"' sample.yml
yq '.a = "cat" | .b = "dog"' sample.yml
```
will output
```yaml

View File

@ -8,7 +8,7 @@ This will, like the `jq` equivalent, recursively match all _value_ nodes. Use it
For instance to set the `style` of all _value_ nodes in a yaml doc, excluding map keys:
```bash
yq eval '.. style= "flow"' file.yaml
yq '.. style= "flow"' file.yaml
```
## match values and map keys form `...`
@ -17,7 +17,7 @@ The also includes map keys in the results set. This is particularly useful in YA
For instance to set the `style` of all nodes in a yaml doc, including the map keys:
```bash
yq eval '... style= "flow"' file.yaml
yq '... style= "flow"' file.yaml
```
## Recurse map (values only)
Given a sample.yml file of:
@ -26,7 +26,7 @@ a: frog
```
then
```bash
yq eval '..' sample.yml
yq '..' sample.yml
```
will output
```yaml
@ -47,7 +47,7 @@ a:
```
then
```bash
yq eval '[.. | select(has("name"))]' sample.yml
yq '[.. | select(has("name"))]' sample.yml
```
will output
```yaml
@ -70,7 +70,7 @@ a:
```
then
```bash
yq eval '.. | select(. == "frog")' sample.yml
yq '.. | select(. == "frog")' sample.yml
```
will output
```yaml
@ -87,7 +87,7 @@ a: frog
```
then
```bash
yq eval '...' sample.yml
yq '...' sample.yml
```
will output
```yaml
@ -105,7 +105,7 @@ b: *cat
```
then
```bash
yq eval '[..]' sample.yml
yq '[..]' sample.yml
```
will output
```yaml
@ -142,7 +142,7 @@ foobar:
```
then
```bash
yq eval '.foobar | [..]' sample.yml
yq '.foobar | [..]' sample.yml
```
will output
```yaml

View File

@ -31,7 +31,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] as $item ireduce (0; . + $item)' sample.yml
yq '.[] as $item ireduce (0; . + $item)' sample.yml
```
will output
```yaml
@ -67,7 +67,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] as $item ireduce ({}; .[$item | .name] = ($item | .has) )' sample.yml
yq '.[] as $item ireduce ({}; .[$item | .name] = ($item | .has) )' sample.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | select(. == "*at")' sample.yml
yq '.[] | select(. == "*at")' sample.yml
```
will output
```yaml
@ -28,7 +28,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | select(. == "go*")' sample.yml
yq '.[] | select(. == "go*")' sample.yml
```
will output
```yaml
@ -46,7 +46,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | select(. == "*go*")' sample.yml
yq '.[] | select(. == "*go*")' sample.yml
```
will output
```yaml
@ -67,7 +67,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.yml
yq '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.yml
```
will output
```yaml
@ -84,7 +84,7 @@ horse: dog
```
then
```bash
yq eval '.[] | select(. == "cat" or test("og$"))' sample.yml
yq '.[] | select(. == "cat" or test("og$"))' sample.yml
```
will output
```yaml
@ -101,7 +101,7 @@ game: poker
```
then
```bash
yq eval 'with_entries(select(.key | test("ame$")))' sample.yml
yq 'with_entries(select(.key | test("ame$")))' sample.yml
```
will output
```yaml
@ -121,7 +121,7 @@ a:
```
then
```bash
yq eval '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.yml
yq '(.a.[] | select(. == "cat" or . == "goat")) |= "rabbit"' sample.yml
```
will output
```yaml

View File

@ -5,11 +5,13 @@ The Sort Keys operator sorts maps by their keys (based on their string value). T
Sort is particularly useful for diffing two different yaml documents:
```bash
yq eval -i -P 'sort_keys(..)' file1.yml
yq eval -i -P 'sort_keys(..)' file2.yml
yq -i -P 'sort_keys(..)' file1.yml
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.
## Sort keys of map
Given a sample.yml file of:
```yaml
@ -19,7 +21,7 @@ b: bing
```
then
```bash
yq eval 'sort_keys(.)' sample.yml
yq 'sort_keys(.)' sample.yml
```
will output
```yaml
@ -49,7 +51,7 @@ aParent:
```
then
```bash
yq eval 'sort_keys(..)' sample.yml
yq 'sort_keys(..)' sample.yml
```
will output
```yaml

View File

@ -13,7 +13,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'sort_by(.a)' sample.yml
yq 'sort_by(.a)' sample.yml
```
will output
```yaml
@ -32,7 +32,7 @@ cool:
```
then
```bash
yq eval '.cool |= sort_by(.a)' sample.yml
yq '.cool |= sort_by(.a)' sample.yml
```
will output
```yaml
@ -54,7 +54,7 @@ cool:
```
then
```bash
yq eval '.cool |= sort_by(keys | .[0])' sample.yml
yq '.cool |= sort_by(keys | .[0])' sample.yml
```
will output
```yaml
@ -80,7 +80,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'sort_by(.a)' sample.yml
yq 'sort_by(.a)' sample.yml
```
will output
```yaml
@ -103,7 +103,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'sort_by(.a)' sample.yml
yq 'sort_by(.a)' sample.yml
```
will output
```yaml
@ -125,7 +125,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'sort' sample.yml
yq 'sort' sample.yml
```
will output
```yaml

View File

@ -5,7 +5,7 @@ This operator splits all matches into separate documents
## Split empty
Running
```bash
yq eval --null-input 'splitDoc'
yq --null-input 'splitDoc'
```
will output
```yaml
@ -20,7 +20,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | splitDoc' sample.yml
yq '.[] | splitDoc' sample.yml
```
will output
```yaml

View File

@ -17,13 +17,13 @@ a: |
Using `$( exp )` wont work, as it will trim the trailing new line.
```
m=$(echo "cat\n") yq e -n '.a = strenv(m)'
m=$(echo "cat\n") yq -n '.a = strenv(m)'
a: cat
```
However, using printf works:
```
printf -v m "cat\n" ; m="$m" yq e -n '.a = strenv(m)'
printf -v m "cat\n" ; m="$m" yq -n '.a = strenv(m)'
a: |
cat
```
@ -31,7 +31,7 @@ a: |
As well as having multiline expressions:
```
m="cat
" yq e -n '.a = strenv(m)'
" yq -n '.a = strenv(m)'
a: |
cat
```
@ -40,7 +40,7 @@ Similarly, if you're trying to set the content from a file, and want a trailing
```
IFS= read -rd '' output < <(cat my_file)
output=$output ./yq e '.data.values = strenv(output)' first.yml
output=$output ./yq '.data.values = strenv(output)' first.yml
```
## Join strings
@ -54,7 +54,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'join("; ")' sample.yml
yq 'join("; ")' sample.yml
```
will output
```yaml
@ -68,7 +68,7 @@ foo bar foo
```
then
```bash
yq eval 'match("foo")' sample.yml
yq 'match("foo")' sample.yml
```
will output
```yaml
@ -85,7 +85,7 @@ foo bar FOO
```
then
```bash
yq eval '[match("(?i)foo"; "g")]' sample.yml
yq '[match("(?i)foo"; "g")]' sample.yml
```
will output
```yaml
@ -106,7 +106,7 @@ abc abc
```
then
```bash
yq eval '[match("(abc)+"; "g")]' sample.yml
yq '[match("(abc)+"; "g")]' sample.yml
```
will output
```yaml
@ -133,7 +133,7 @@ foo bar foo foo foo
```
then
```bash
yq eval '[match("foo (?P<bar123>bar)? foo"; "g")]' sample.yml
yq '[match("foo (?P<bar123>bar)? foo"; "g")]' sample.yml
```
will output
```yaml
@ -162,7 +162,7 @@ xyzzy-14
```
then
```bash
yq eval 'capture("(?P<a>[a-z]+)-(?P<n>[0-9]+)")' sample.yml
yq 'capture("(?P<a>[a-z]+)-(?P<n>[0-9]+)")' sample.yml
```
will output
```yaml
@ -177,7 +177,7 @@ cat cat
```
then
```bash
yq eval 'match("cat")' sample.yml
yq 'match("cat")' sample.yml
```
will output
```yaml
@ -194,7 +194,7 @@ cat cat
```
then
```bash
yq eval '[match("cat"; "g")]' sample.yml
yq '[match("cat"; "g")]' sample.yml
```
will output
```yaml
@ -218,7 +218,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] | test("at")' sample.yml
yq '.[] | test("at")' sample.yml
```
will output
```yaml
@ -236,7 +236,7 @@ a: dogs are great
```
then
```bash
yq eval '.a |= sub("dogs", "cats")' sample.yml
yq '.a |= sub("dogs", "cats")' sample.yml
```
will output
```yaml
@ -254,7 +254,7 @@ b: heat
```
then
```bash
yq eval '.[] |= sub("(a)", "${1}r")' sample.yml
yq '.[] |= sub("(a)", "${1}r")' sample.yml
```
will output
```yaml
@ -269,7 +269,7 @@ cat; meow; 1; ; true
```
then
```bash
yq eval 'split("; ")' sample.yml
yq 'split("; ")' sample.yml
```
will output
```yaml
@ -287,7 +287,7 @@ word
```
then
```bash
yq eval 'split("; ")' sample.yml
yq 'split("; ")' sample.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ a:
```
then
```bash
yq eval '.a.b = "new" | .a.b style="double"' sample.yml
yq '.a.b = "new" | .a.b style="double"' sample.yml
```
will output
```yaml
@ -29,7 +29,7 @@ a:
```
then
```bash
yq eval 'with(.a.b ; . = "new" | . style="double")' sample.yml
yq 'with(.a.b ; . = "new" | . style="double")' sample.yml
```
will output
```yaml
@ -48,7 +48,7 @@ e: true
```
then
```bash
yq eval '.. style="tagged"' sample.yml
yq '.. style="tagged"' sample.yml
```
will output
```yaml
@ -69,7 +69,7 @@ e: true
```
then
```bash
yq eval '.. style="double"' sample.yml
yq '.. style="double"' sample.yml
```
will output
```yaml
@ -89,7 +89,7 @@ e: true
```
then
```bash
yq eval '... style="double"' sample.yml
yq '... style="double"' sample.yml
```
will output
```yaml
@ -109,7 +109,7 @@ e: true
```
then
```bash
yq eval '.. style="single"' sample.yml
yq '.. style="single"' sample.yml
```
will output
```yaml
@ -129,7 +129,7 @@ e: true
```
then
```bash
yq eval '.. style="literal"' sample.yml
yq '.. style="literal"' sample.yml
```
will output
```yaml
@ -153,7 +153,7 @@ e: true
```
then
```bash
yq eval '.. style="folded"' sample.yml
yq '.. style="folded"' sample.yml
```
will output
```yaml
@ -177,7 +177,7 @@ e: true
```
then
```bash
yq eval '.. style="flow"' sample.yml
yq '.. style="flow"' sample.yml
```
will output
```yaml
@ -196,7 +196,7 @@ a: cat
```
then
```bash
yq eval '... style=""' sample.yml
yq '... style=""' sample.yml
```
will output
```yaml
@ -214,7 +214,7 @@ b: double
```
then
```bash
yq eval '.[] style |= .' sample.yml
yq '.[] style |= .' sample.yml
```
will output
```yaml
@ -229,7 +229,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.. | style' sample.yml
yq '.. | style' sample.yml
```
will output
```yaml

View File

@ -5,7 +5,7 @@ You can use subtract to subtract numbers, as well as removing elements from an a
## Array subtraction
Running
```bash
yq eval --null-input '[1,2] - [2,3]'
yq --null-input '[1,2] - [2,3]'
```
will output
```yaml
@ -15,7 +15,7 @@ will output
## Array subtraction with nested array
Running
```bash
yq eval --null-input '[[1], 1, 2] - [[1], 3]'
yq --null-input '[[1], 1, 2] - [[1], 3]'
```
will output
```yaml
@ -34,7 +34,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '. - [{"c": "d", "a": "b"}]' sample.yml
yq '. - [{"c": "d", "a": "b"}]' sample.yml
```
will output
```yaml
@ -51,7 +51,7 @@ b: 4.5
```
then
```bash
yq eval '.a = .a - .b' sample.yml
yq '.a = .a - .b' sample.yml
```
will output
```yaml
@ -69,7 +69,7 @@ b: 4.5
```
then
```bash
yq eval '.a = .a - .b' sample.yml
yq '.a = .a - .b' sample.yml
```
will output
```yaml
@ -87,7 +87,7 @@ b: 4
```
then
```bash
yq eval '.a = .a - .b' sample.yml
yq '.a = .a - .b' sample.yml
```
will output
```yaml
@ -103,7 +103,7 @@ b: 5
```
then
```bash
yq eval '.[] -= 1' sample.yml
yq '.[] -= 1' sample.yml
```
will output
```yaml
@ -111,3 +111,21 @@ a: 2
b: 4
```
## Custom types: that are really numbers
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
```yaml
a: !horse 2
b: !goat 1
```
then
```bash
yq '.a -= .b' sample.yml
```
will output
```yaml
a: !horse 1
b: !goat 1
```

View File

@ -13,7 +13,7 @@ f: []
```
then
```bash
yq eval '.. | tag' sample.yml
yq '.. | tag' sample.yml
```
will output
```yaml
@ -32,7 +32,7 @@ a: str
```
then
```bash
yq eval '.a tag = "!!mikefarah"' sample.yml
yq '.a tag = "!!mikefarah"' sample.yml
```
will output
```yaml
@ -49,7 +49,7 @@ e: true
```
then
```bash
yq eval '(.. | select(tag == "!!int")) tag= "!!str"' sample.yml
yq '(.. | select(tag == "!!int")) tag= "!!str"' sample.yml
```
will output
```yaml

View File

@ -10,7 +10,7 @@ a:
```
then
```bash
yq eval '.a' sample.yml
yq '.a' sample.yml
```
will output
```yaml
@ -27,7 +27,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[]' sample.yml
yq '.[]' sample.yml
```
will output
```yaml
@ -44,7 +44,7 @@ cat
```
then
```bash
yq eval '.[]' sample.yml
yq '.[]' sample.yml
```
will output
```yaml
@ -59,7 +59,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.["{}"]' sample.yml
yq '.["{}"]' sample.yml
```
will output
```yaml
@ -75,7 +75,7 @@ a:
```
then
```bash
yq eval '.a["key.withdots"]["another.key"]' sample.yml
yq '.a["key.withdots"]["another.key"]' sample.yml
```
will output
```yaml
@ -91,7 +91,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.["red rabbit"]' sample.yml
yq '.["red rabbit"]' sample.yml
```
will output
```yaml
@ -109,7 +109,7 @@ banana: soft yum
```
then
```bash
yq eval '.[.b]' sample.yml
yq '.[.b]' sample.yml
```
will output
```yaml
@ -125,7 +125,7 @@ c: banana
```
then
```bash
yq eval '.a.b' sample.yml
yq '.a.b' sample.yml
```
will output
```yaml
@ -143,7 +143,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.a?' sample.yml
yq '.a?' sample.yml
```
will output
```yaml
@ -158,7 +158,7 @@ a:
```
then
```bash
yq eval '.a."*a*"' sample.yml
yq '.a."*a*"' sample.yml
```
will output
```yaml
@ -175,7 +175,7 @@ b: *cat
```
then
```bash
yq eval '.b' sample.yml
yq '.b' sample.yml
```
will output
```yaml
@ -191,7 +191,7 @@ b: *cat
```
then
```bash
yq eval '.b[]' sample.yml
yq '.b[]' sample.yml
```
will output
```yaml
@ -207,7 +207,7 @@ b: *cat
```
then
```bash
yq eval '.b.c' sample.yml
yq '.b.c' sample.yml
```
will output
```yaml
@ -223,7 +223,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[0]' sample.yml
yq '.[0]' sample.yml
```
will output
```yaml
@ -237,7 +237,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[1][0]' sample.yml
yq '.[1][0]' sample.yml
```
will output
```yaml
@ -251,7 +251,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[2]' sample.yml
yq '.[2]' sample.yml
```
will output
```yaml
@ -265,7 +265,7 @@ a: b
```
then
```bash
yq eval '.[0]' sample.yml
yq '.[0]' sample.yml
```
will output
```yaml
@ -296,7 +296,7 @@ foobar:
```
then
```bash
yq eval '.foobar.a' sample.yml
yq '.foobar.a' sample.yml
```
will output
```yaml
@ -327,7 +327,7 @@ foobar:
```
then
```bash
yq eval '.foobar.c' sample.yml
yq '.foobar.c' sample.yml
```
will output
```yaml
@ -358,7 +358,7 @@ foobar:
```
then
```bash
yq eval '.foobar.thing' sample.yml
yq '.foobar.thing' sample.yml
```
will output
```yaml
@ -389,7 +389,7 @@ foobar:
```
then
```bash
yq eval '.foobar[]' sample.yml
yq '.foobar[]' sample.yml
```
will output
```yaml
@ -424,7 +424,7 @@ foobar:
```
then
```bash
yq eval '.foobarList.thing' sample.yml
yq '.foobarList.thing' sample.yml
```
will output
```yaml
@ -455,7 +455,7 @@ foobar:
```
then
```bash
yq eval '.foobarList[]' sample.yml
yq '.foobarList[]' sample.yml
```
will output
```yaml
@ -475,7 +475,7 @@ a:
```
then
```bash
yq eval '.a[0, 2]' sample.yml
yq '.a[0, 2]' sample.yml
```
will output
```yaml

View File

@ -5,7 +5,7 @@ This operator is used to combine different results together.
## Combine scalars
Running
```bash
yq eval --null-input '1, true, "cat"'
yq --null-input '1, true, "cat"'
```
will output
```yaml
@ -23,7 +23,7 @@ c: fieldC
```
then
```bash
yq eval '.a, .c' sample.yml
yq '.a, .c' sample.yml
```
will output
```yaml

View File

@ -12,7 +12,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'unique' sample.yml
yq 'unique' sample.yml
```
will output
```yaml
@ -33,7 +33,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'unique' sample.yml
yq 'unique' sample.yml
```
will output
```yaml
@ -53,7 +53,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'unique_by(tag)' sample.yml
yq 'unique_by(tag)' sample.yml
```
will output
```yaml
@ -72,7 +72,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval 'unique_by(.name)' sample.yml
yq 'unique_by(.name)' sample.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ a: cat
```
then
```bash
yq eval '.a as $foo | $foo' sample.yml
yq '.a as $foo | $foo' sample.yml
```
will output
```yaml
@ -26,7 +26,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.[] as $foo | $foo' sample.yml
yq '.[] as $foo | $foo' sample.yml
```
will output
```yaml
@ -50,7 +50,7 @@ Given a sample.yml file of:
```
then
```bash
yq eval '.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}' sample.yml
yq '.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}' sample.yml
```
will output
```yaml
@ -68,7 +68,7 @@ b: b_value
```
then
```bash
yq eval '.a as $x | .b as $y | .b = $x | .a = $y' sample.yml
yq '.a as $x | .b as $y | .b = $x | .a = $y' sample.yml
```
will output
```yaml
@ -87,7 +87,7 @@ a:
```
then
```bash
yq eval '.a.b ref $x | $x = "new" | $x style="double"' sample.yml
yq '.a.b ref $x | $x = "new" | $x style="double"' sample.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ a:
```
then
```bash
yq eval 'with(.a.deeply.nested; . = "newValue" | . style="single")' sample.yml
yq 'with(.a.deeply.nested; . = "newValue" | . style="single")' sample.yml
```
will output
```yaml
@ -30,7 +30,7 @@ a:
```
then
```bash
yq eval 'with(.a.deeply; .nested = "newValue" | .other= "newThing")' sample.yml
yq 'with(.a.deeply; .nested = "newValue" | .other= "newThing")' sample.yml
```
will output
```yaml
@ -51,7 +51,7 @@ myArray:
```
then
```bash
yq eval 'with(.myArray[]; .b = .a + " yum")' sample.yml
yq 'with(.myArray[]; .b = .a + " yum")' sample.yml
```
will output
```yaml

View File

@ -13,7 +13,7 @@ Given a sample.json file of:
```
then
```bash
yq e -P '.' sample.json
yq -P '.' sample.json
```
will output
```yaml
@ -29,7 +29,7 @@ Given a sample.json file of:
```
then
```bash
yq e -P '.' sample.json
yq -P '.' sample.json
```
will output
```yaml
@ -48,7 +48,7 @@ cat: meow
```
then
```bash
yq e -o=json '.' sample.yml
yq -o=json '.' sample.yml
```
will output
```json
@ -64,7 +64,7 @@ cat: meow # this is a comment, and it will be dropped.
```
then
```bash
yq e -o=json -I=0 '.' sample.yml
yq -o=json -I=0 '.' sample.yml
```
will output
```json
@ -78,7 +78,7 @@ cat: meow # this is a comment, and it will be dropped.
```
then
```bash
yq e -o=json '.' sample.yml
yq -o=json '.' sample.yml
```
will output
```json
@ -97,7 +97,7 @@ anotherCat: *ref
```
then
```bash
yq e -o=json '.' sample.yml
yq -o=json '.' sample.yml
```
will output
```json
@ -116,7 +116,7 @@ things: [{stuff: cool}, {whatever: cat}]
```
then
```bash
yq e -o=json -I=0 '.things[]' sample.yml
yq -o=json -I=0 '.things[]' sample.yml
```
will output
```json

View File

@ -20,7 +20,7 @@ Given a sample.xml file of:
```
then
```bash
yq e -p=xml '.' sample.xml
yq -p=xml '.' sample.xml
```
will output
```yaml
@ -44,7 +44,7 @@ Given a sample.xml file of:
```
then
```bash
yq e -p=xml ' (.. | select(tag == "!!str")) |= from_yaml' sample.xml
yq -p=xml ' (.. | select(tag == "!!str")) |= from_yaml' sample.xml
```
will output
```yaml
@ -65,7 +65,7 @@ Given a sample.xml file of:
```
then
```bash
yq e -p=xml '.' sample.xml
yq -p=xml '.' sample.xml
```
will output
```yaml
@ -86,7 +86,7 @@ Given a sample.xml file of:
```
then
```bash
yq e -p=xml '.' sample.xml
yq -p=xml '.' sample.xml
```
will output
```yaml
@ -105,7 +105,7 @@ Given a sample.xml file of:
```
then
```bash
yq e -p=xml '.' sample.xml
yq -p=xml '.' sample.xml
```
will output
```yaml
@ -140,7 +140,7 @@ for x --></x>
```
then
```bash
yq e -p=xml '.' sample.xml
yq -p=xml '.' sample.xml
```
will output
```yaml
@ -168,7 +168,7 @@ cat: purrs
```
then
```bash
yq e -o=xml '.' sample.yml
yq -o=xml '.' sample.yml
```
will output
```xml
@ -185,7 +185,7 @@ pets:
```
then
```bash
yq e -o=xml '.' sample.yml
yq -o=xml '.' sample.yml
```
will output
```xml
@ -207,7 +207,7 @@ cat:
```
then
```bash
yq e -o=xml '.' sample.yml
yq -o=xml '.' sample.yml
```
will output
```xml
@ -228,7 +228,7 @@ cat:
```
then
```bash
yq e -o=xml '.' sample.yml
yq -o=xml '.' sample.yml
```
will output
```xml
@ -252,7 +252,7 @@ cat: # inline_cat
```
then
```bash
yq e -o=xml '.' sample.yml
yq -o=xml '.' sample.yml
```
will output
```xml
@ -288,7 +288,7 @@ for x --></x>
```
then
```bash
yq e -p=xml -o=xml '.' sample.xml
yq -p=xml -o=xml '.' sample.xml
```
will output
```xml