-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
378 additions
and
157 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
general/data-transform.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# summary | ||
# Summary | ||
|
||
```jq | ||
def grouped_summary($item): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Text functions | ||
|
||
## Recursively split strings w/ new lines | ||
|
||
```jq | ||
def split_newlines($s): | ||
if ((type == "string") and (($s|tostring|split("\n")|length) > 1)?) | ||
then ($s|tostring|split("[\\r\\n]+([\\s]+)?";"x")) | ||
elif (type == "object") then to_entries | ||
else $s end; | ||
def recuse_split_newlines: walk(split_newlines(.)|from_entries? // .); | ||
``` | ||
|
||
## Quoting | ||
|
||
```jq | ||
def squo: [39]|implode; | ||
def squote($text): [squo,$text,squo]|join(""); | ||
def dquote($text): "\"\($text)\""; | ||
def unsmart($text): $text | gsub("[“”]";"\"") | gsub("[’‘]";"'"); | ||
def unsmart: . | unsmart; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Data transformation | ||
|
||
## json2ini | ||
|
||
Adapted from <a>https://stackoverflow.com/a/76665197</a> | ||
|
||
Data used: [nested.json](data/nested.json) | ||
|
||
``` | ||
{ | ||
"server": { | ||
"atx": { | ||
"user": "annie", | ||
"port": 22 | ||
} | ||
}, | ||
"storage": { | ||
"nyc": { | ||
"user": "nntrn", | ||
"port": 22 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```sh | ||
jq --stream -nr ' reduce (inputs | select(has(1))) as [$path, $val] | ||
( {}; .[$path[:-1] | join(".")][$path[-1]] = $val ) | ||
| to_entries[] | ||
| "[\(.key)]", (.value | to_entries[] | "\(.key) = \(.value)" ) | ||
, ""' | ||
``` | ||
|
||
Output | ||
|
||
``` | ||
[server.atx] | ||
user = annie | ||
port = 22 | ||
[storage.nyc] | ||
user = nntrn | ||
port = 22 | ||
``` | ||
|
||
## tsv2json | ||
|
||
Adapted from <a>https://stackoverflow.com/a/55996042</a> | ||
|
||
Data used: [tmp.tsv](data/tmp.tsv) | ||
|
||
``` | ||
foo bar baz | ||
1 2 3 | ||
4 5 6 | ||
``` | ||
|
||
```sh | ||
jq -R 'split("[\\s\\t]+";"x") as $head | ||
| [inputs | split("[\\s\\t]+";"x")] | ||
| map( . as $row | reduce (keys|.[]) as $x | ||
( {}; . + {"\($head[$x])":$row[$x]} ) )' data/tmp.tsv | ||
``` | ||
|
||
Output | ||
|
||
```json | ||
[ | ||
{ | ||
"foo": "1", | ||
"bar": "2", | ||
"baz": "3" | ||
}, | ||
{ | ||
"foo": "4", | ||
"bar": "5", | ||
"baz": "6" | ||
} | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.