diff --git a/SUMMARY.md b/SUMMARY.md index b56f044b..c5735e1b 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -48,6 +48,7 @@ * [Map](operators/map.md) * [Modulo](operators/modulo.md) * [Multiply (Merge)](operators/multiply-merge.md) + * [Omit](operators/omit.md) * [Parent](operators/parent.md) * [Path](operators/path.md) * [Pick](operators/pick.md) diff --git a/operators/multiply-merge.md b/operators/multiply-merge.md index 963a1f30..368d91fa 100644 --- a/operators/multiply-merge.md +++ b/operators/multiply-merge.md @@ -55,6 +55,62 @@ a: 12 b: 4 ``` +## Multiply string node X int +Given a sample.yml file of: +```yaml +b: banana +``` +then +```bash +yq '.b * 4' sample.yml +``` +will output +```yaml +bananabananabananabanana +``` + +## Multiply int X string node +Given a sample.yml file of: +```yaml +b: banana +``` +then +```bash +yq '4 * .b' sample.yml +``` +will output +```yaml +bananabananabananabanana +``` + +## Multiply string X int node +Given a sample.yml file of: +```yaml +n: 4 +``` +then +```bash +yq '"banana" * .n' sample.yml +``` +will output +```yaml +bananabananabananabanana +``` + +## Multiply int node X string +Given a sample.yml file of: +```yaml +n: 4 +``` +then +```bash +yq '.n * "banana"' sample.yml +``` +will output +```yaml +bananabananabananabanana +``` + ## Merge objects together, returning merged result only Given a sample.yml file of: ```yaml diff --git a/operators/omit.md b/operators/omit.md new file mode 100644 index 00000000..56a7f70d --- /dev/null +++ b/operators/omit.md @@ -0,0 +1,44 @@ +# Omit + +Works like `pick`, but instead you specify the keys/indices that you _don't_ want included. + +## Omit keys from map +Note that non existent keys are skipped. + +Given a sample.yml file of: +```yaml +myMap: + cat: meow + dog: bark + thing: hamster + hamster: squeak +``` +then +```bash +yq '.myMap |= omit(["hamster", "cat", "goat"])' sample.yml +``` +will output +```yaml +myMap: + dog: bark + thing: hamster +``` + +## Omit indices from array +Note that non existent indices are skipped. + +Given a sample.yml file of: +```yaml +- cat +- leopard +- lion +``` +then +```bash +yq 'omit([2, 0, 734, -5])' sample.yml +``` +will output +```yaml +- leopard +``` + diff --git a/operators/parent.md b/operators/parent.md index 0ef18d55..9bdc6822 100644 --- a/operators/parent.md +++ b/operators/parent.md @@ -37,6 +37,43 @@ fruit: banana name: sam ``` +## N-th parent +You can optionally supply the number of levels to go up for the parent, the default being 1. + +Given a sample.yml file of: +```yaml +a: + b: + c: cat +``` +then +```bash +yq '.a.b.c | parent(2)' sample.yml +``` +will output +```yaml +b: + c: cat +``` + +## N-th parent - another level +Given a sample.yml file of: +```yaml +a: + b: + c: cat +``` +then +```bash +yq '.a.b.c | parent(3)' sample.yml +``` +will output +```yaml +a: + b: + c: cat +``` + ## No parent Given a sample.yml file of: ```yaml diff --git a/operators/string-operators.md b/operators/string-operators.md index 6aa854bc..e76c8860 100644 --- a/operators/string-operators.md +++ b/operators/string-operators.md @@ -56,6 +56,40 @@ IFS= read -rd '' output < <(cat my_file) output=$output ./yq '.data.values = strenv(output)' first.yml ``` +## Interpolation +Given a sample.yml file of: +```yaml +value: things +another: stuff +``` +then +```bash +yq '.message = "I like \(.value) and \(.another)"' sample.yml +``` +will output +```yaml +value: things +another: stuff +message: I like things and stuff +``` + +## Interpolation - not a string +Given a sample.yml file of: +```yaml +value: + an: apple +``` +then +```bash +yq '.message = "I like \(.value)"' sample.yml +``` +will output +```yaml +value: + an: apple +message: 'I like an: apple' +``` + ## To up (upper) case Works with unicode characters @@ -383,3 +417,32 @@ will output - word ``` +## To string +Note that you may want to force `yq` to leave scalar values wrapped by passing in `--unwrapScalar=false` or `-r=f` + +Given a sample.yml file of: +```yaml +- 1 +- true +- null +- ~ +- cat +- an: object +- - array + - 2 +``` +then +```bash +yq '.[] |= to_string' sample.yml +``` +will output +```yaml +- "1" +- "true" +- "null" +- "~" +- cat +- "an: object" +- "- array\n- 2" +``` +