diff --git a/operators/boolean-operators.md b/operators/boolean-operators.md index 887817d7..15327666 100644 --- a/operators/boolean-operators.md +++ b/operators/boolean-operators.md @@ -4,7 +4,7 @@ The `or` and `and` operators take two parameters and return a boolean result. `not` flips a boolean from true to false, or vice versa. -`any` will return `true` if there are any `true` values in a array sequence, and `all` will return true if _all_ elements in an array are true. +`any` will return `true` if there are any `true` values in an array sequence, and `all` will return true if _all_ elements in an array are true. `any_c(condition)` and `all_c(condition)` are like `any` and `all` but they take a condition expression that is used against each element to determine if it's `true`. Note: in `jq` you can simply pass a condition to `any` or `all` and it simply works - `yq` isn't that clever..yet diff --git a/operators/comment-operators.md b/operators/comment-operators.md index 89af3b1b..414fff83 100644 --- a/operators/comment-operators.md +++ b/operators/comment-operators.md @@ -187,7 +187,6 @@ yq '. head_comment="single"' sample.yml will output ```yaml # single - a: cat ``` diff --git a/operators/create-collect-into-object.md b/operators/create-collect-into-object.md index 2c132bb8..ff0c9cbe 100644 --- a/operators/create-collect-into-object.md +++ b/operators/create-collect-into-object.md @@ -66,6 +66,7 @@ will output ```yaml Mike: cat Mike: dog +--- Rosey: monkey Rosey: sheep ``` diff --git a/operators/document-index.md b/operators/document-index.md index 619fd391..ebe994e5 100644 --- a/operators/document-index.md +++ b/operators/document-index.md @@ -85,6 +85,7 @@ will output ```yaml match: cat doc: 0 +--- match: frog doc: 1 ``` diff --git a/operators/file-operators.md b/operators/file-operators.md index b1dacd78..8c331b83 100644 --- a/operators/file-operators.md +++ b/operators/file-operators.md @@ -54,7 +54,6 @@ yq eval-all 'file_index' sample.yml another.yml will output ```yaml 0 ---- 1 ``` diff --git a/operators/kind.md b/operators/kind.md new file mode 100644 index 00000000..1f65dfbb --- /dev/null +++ b/operators/kind.md @@ -0,0 +1,77 @@ + +## Get kind +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +f: [] +g: {} +h: null +``` +then +```bash +yq '.. | kind' sample.yml +``` +will output +```yaml +map +scalar +scalar +scalar +scalar +seq +map +scalar +``` + +## Get kind, ignores custom tags +Unlike tag, kind is not affected by custom tags. + +Given a sample.yml file of: +```yaml +a: !!thing cat +b: !!foo {} +c: !!bar [] +``` +then +```bash +yq '.. | kind' sample.yml +``` +will output +```yaml +map +scalar +map +seq +``` + +## Add comments only to scalars +An example of how you can use kind + +Given a sample.yml file of: +```yaml +a: + b: 5 + c: 3.2 +e: true +f: [] +g: {} +h: null +``` +then +```bash +yq '(.. | select(kind == "scalar")) line_comment = "this is a scalar"' sample.yml +``` +will output +```yaml +a: + b: 5 # this is a scalar + c: 3.2 # this is a scalar +e: true # this is a scalar +f: [] +g: {} +h: null # this is a scalar +``` + diff --git a/operators/multiply-merge.md b/operators/multiply-merge.md index 3d4a67e1..a7cf3d62 100644 --- a/operators/multiply-merge.md +++ b/operators/multiply-merge.md @@ -519,7 +519,7 @@ will output some: thing ``` -## Merging an null with an array +## Merging a null with an array Running ```bash yq --null-input 'null * ["some"]' diff --git a/operators/to_number.md b/operators/to_number.md new file mode 100644 index 00000000..2e329033 --- /dev/null +++ b/operators/to_number.md @@ -0,0 +1,49 @@ +# To Number +Parses the input as a number. yq will try to parse values as an int first, failing that it will try float. Values that already ints or floats will be left alone. + +## Converts strings to numbers +Given a sample.yml file of: +```yaml +- "3" +- "3.1" +- "-1e3" +``` +then +```bash +yq '.[] | to_number' sample.yml +``` +will output +```yaml +3 +3.1 +-1e3 +``` + +## Doesn't change numbers +Given a sample.yml file of: +```yaml +- 3 +- 3.1 +- -1e3 +``` +then +```bash +yq '.[] | to_number' sample.yml +``` +will output +```yaml +3 +3.1 +-1e3 +``` + +## Cannot convert null +Running +```bash +yq --null-input '.a.b | to_number' +``` +will output +```bash +Error: cannot convert node value [null] at path a.b of tag !!null to number +``` + diff --git a/usage/convert.md b/usage/convert.md index 1f149c11..5060f8da 100644 --- a/usage/convert.md +++ b/usage/convert.md @@ -14,7 +14,7 @@ Given a sample.json file of: ``` then ```bash -yq -P '.' sample.json +yq -p=json sample.json ``` will output ```yaml @@ -30,16 +30,16 @@ Given a sample.json file of: ``` then ```bash -yq -P '.' sample.json +yq -p=json sample.json ``` will output ```yaml a: Easy! as one two three b: - c: 2 - d: - - 3 - - 4 + c: 2 + d: + - 3 + - 4 ``` ## Encode json: simple diff --git a/usage/lua.md b/usage/lua.md index 61458be2..6b6dcc8b 100644 --- a/usage/lua.md +++ b/usage/lua.md @@ -1,5 +1,33 @@ -## Basic example +## Basic input example +Given a sample.lua file of: +```lua +return { + ["country"] = "Australia"; -- this place + ["cities"] = { + "Sydney", + "Melbourne", + "Brisbane", + "Perth", + }; +}; + +``` +then +```bash +yq -oy '.' sample.lua +``` +will output +```yaml +country: Australia +cities: + - Sydney + - Melbourne + - Brisbane + - Perth +``` + +## Basic output example Given a sample.yml file of: ```yaml --- @@ -101,6 +129,8 @@ numbers: - octal: 0o30 - float: 123.45 - infinity: .inf + plus_infinity: +.inf + minus_infinity: -.inf - not: .nan ``` @@ -134,6 +164,8 @@ return { }, { ["infinity"] = (1/0); + ["plus_infinity"] = (1/0); + ["minus_infinity"] = (-1/0); }, { ["not"] = (0/0); diff --git a/usage/xml.md b/usage/xml.md index 2d469450..503f9a74 100644 --- a/usage/xml.md +++ b/usage/xml.md @@ -129,15 +129,13 @@ zoo: ``` ## Parse xml: force all as an array -Because of the way yq works, when updating everything you need to update the children before the parents. By default `..` will match parents first, so we reverse that before updating. - Given a sample.xml file of: ```xml boing ``` then ```bash -yq -oy '([..] | reverse | .[]) |= [] + .' sample.xml +yq -oy '.. |= [] + .' sample.xml ``` will output ```yaml