From 814a6114a8c1a4eee480fd1aee4c6b7f9d03be09 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 20 Feb 2023 16:08:51 +1100 Subject: [PATCH] v4.31.1 --- SUMMARY.md | 1 + operators/datetime.md | 24 ++++++++++++++++++ operators/encode-decode.md | 46 ++++++++++++++++++++++++++++++++++ operators/shuffle.md | 51 ++++++++++++++++++++++++++++++++++++++ operators/sort.md | 22 ++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 operators/shuffle.md diff --git a/SUMMARY.md b/SUMMARY.md index 16fb15a0..88d77525 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -51,6 +51,7 @@ * [Reduce](operators/reduce.md) * [Reverse](operators/reverse.md) * [Select](operators/select.md) + * [Shuffle](operators/shuffle.md) * [Slice Array](operators/slice-array.md) * [Sort](operators/sort.md) * [Sort Keys](operators/sort-keys.md) diff --git a/operators/datetime.md b/operators/datetime.md index 0dc3ed5e..bd40053e 100644 --- a/operators/datetime.md +++ b/operators/datetime.md @@ -86,6 +86,30 @@ a: cool updated: 2021-05-19T01:02:03Z ``` +## From Unix +Converts from unix time. Note, you don't have to pipe through the tz operator :) + +Running +```bash +yq --null-input '1675301929 | from_unix | tz("UTC")' +``` +will output +```yaml +2023-02-02T01:38:49Z +``` + +## To Unix +Converts to unix time + +Running +```bash +yq --null-input 'now | to_unix' +``` +will output +```yaml +1621386123 +``` + ## Timezone: from standard RFC3339 format Returns a new datetime in the specified timezone. Specify standard IANA Time Zone format or 'utc', 'local'. When given a single parameter, this assumes the datetime is in RFC3339 format. diff --git a/operators/encode-decode.md b/operators/encode-decode.md index f9e5097d..b8cc1f75 100644 --- a/operators/encode-decode.md +++ b/operators/encode-decode.md @@ -16,6 +16,8 @@ These operators are useful to process yaml documents that have stringified embed | TSV | from_tsv/@tsvd | to_tsv/@tsv | | XML | from_xml/@xmld | to_xml(i)/@xml | | Base64 | @base64d | @base64 | +| URI | @urid | @uri | +| Shell | | @sh | See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats. @@ -435,6 +437,50 @@ will output YTogYXBwbGUK ``` +## Encode a string to uri +Given a sample.yml file of: +```yaml +coolData: this has & special () characters * +``` +then +```bash +yq '.coolData | @uri' sample.yml +``` +will output +```yaml +this+has+%26+special+%28%29+characters+%2A +``` + +## Decode a URI to a string +Given a sample.yml file of: +```yaml +this+has+%26+special+%28%29+characters+%2A +``` +then +```bash +yq '@urid' sample.yml +``` +will output +```yaml +this has & special () characters * +``` + +## Encode a string to sh +Sh/Bash friendly string + +Given a sample.yml file of: +```yaml +coolData: strings with spaces and a 'quote' +``` +then +```bash +yq '.coolData | @sh' sample.yml +``` +will output +```yaml +strings' with spaces and a '\'quote\' +``` + ## Decode a base64 encoded string Decoded data is assumed to be a string. diff --git a/operators/shuffle.md b/operators/shuffle.md new file mode 100644 index 00000000..953b513f --- /dev/null +++ b/operators/shuffle.md @@ -0,0 +1,51 @@ +# Shuffle + +Shuffles an array. Note that this command does _not_ use a cryptographically secure random number generator to randomise the array order. + + +## Shuffle array +Given a sample.yml file of: +```yaml +- 1 +- 2 +- 3 +- 4 +- 5 +``` +then +```bash +yq 'shuffle' sample.yml +``` +will output +```yaml +- 5 +- 2 +- 4 +- 1 +- 3 +``` + +## Shuffle array in place +Given a sample.yml file of: +```yaml +cool: + - 1 + - 2 + - 3 + - 4 + - 5 +``` +then +```bash +yq '.cool |= shuffle' sample.yml +``` +will output +```yaml +cool: + - 5 + - 2 + - 4 + - 1 + - 3 +``` + diff --git a/operators/sort.md b/operators/sort.md index 51c79e4a..72a17305 100644 --- a/operators/sort.md +++ b/operators/sort.md @@ -25,6 +25,28 @@ will output - a: cat ``` +## Sort by multiple fields +Given a sample.yml file of: +```yaml +- a: dog +- a: cat + b: banana +- a: cat + b: apple +``` +then +```bash +yq 'sort_by(.a, .b)' sample.yml +``` +will output +```yaml +- a: cat + b: apple +- a: cat + b: banana +- a: dog +``` + ## Sort descending by string field Use sort with reverse to sort in descending order.