-
Notifications
You must be signed in to change notification settings - Fork 213
Tips and Tricks
jc
comes with a few generic or standard-format converters, including:
- ASCII (and Unicode) Tables
- Multi-line ASCII (and Unicode) Tables
- CSV
- INI
- Key/Value
- XML
- YAML
Sometimes commands will output in one of these supported formats for easy conversion to JSON.
For example, you can parse the HTTP headers from curl
using the Key/Value parser:
$ curl --silent --header "origin: https://origin.example.com" \
--request GET "https://cors-test.appspot.com/test" \
--head | jc --kv -p
{
"http/2 200": "",
"cache-control": "no-cache",
"access-control-allow-origin": "https://origin.example.com",
"access-control-max-age": "0",
"access-control-allow-credentials": "true",
"set-cookie": "test=test",
"expires": "Fri, 01 Jan 1990 00:00:00 GMT",
"content-type": "application/json",
"x-cloud-trace-context": "086f2570a400102ed316c10f1719c87e",
"date": "Tue, 17 May 2022 21:39:06 GMT",
"server": "Google Frontend",
"content-length": "15",
"alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""
}
Most parsers have slightly different output when using the -r
(Raw) cli option. The Raw output
is typically used for troubleshooting or in cases where you don't want string to integer/float/bool
conversions or even some calculated timestamp fields.
A couple parsers will output significantly different styles of JSON if the Raw option is used:
env
history
For example, the env
parser turns each environment variable into its own JSON object and adds all of these objects to a list:
$ env | jc --env -p
[
{
"name": "TERM_PROGRAM",
"value": "Apple_Terminal"
},
{
"name": "SHELL",
"value": "/bin/bash"
},
{
"name": "TERM",
"value": "xterm-256color"
},
...
]
This output makes it easy to iterate over each key/value pair and match values to predictable key names. But if you don't need that format, try the -r
option:
$ env | jc --env -p -r
{
"TERM_PROGRAM": "Apple_Terminal",
"SHELL": "/bin/bash",
"TERM": "xterm-256color",
...
}
With this output there is no list to iterate over - If you know the name of the key, you can quickly get the value.
The history
parser is similar:
$ history | jc --history -p
[
{
"line": 1009,
"command": "jc git log | jq"
},
{
"line": 1010,
"command": "jc git log --format=full | jq"
},
{
"line": 1011,
"command": "jc git log --format=fuller --stat | jq"
},
{
"line": 1012,
"command": "jc git log --format=oneline --shortstat | jq"
}
]
...
vs.
$ history | jc --history -p -r
{
"1009": "jc git log | jq",
"1010": "jc git log --format=full | jq",
"1011": "jc git log --format=fuller --stat | jq",
"1012": "jc git log --format=oneline --shortstat | jq"
}