Skip to content

Instantly share code, notes, and snippets.

@ledjon
Created July 30, 2018 20:06
Show Gist options
  • Save ledjon/a95f7267a5319e32f0e4ca4080bf9bb0 to your computer and use it in GitHub Desktop.
Save ledjon/a95f7267a5319e32f0e4ca4080bf9bb0 to your computer and use it in GitHub Desktop.
try to json *nix tool output
I know this is a really old topic: but maybe one way to address this problem is to have a general purpose tool that does a "pretty good" job of converting text-based output to structured output.
The reality is that most *nix tools output in a way that is designed for the human mind to "parse", so there is probably a detectable structure to the output that a smart tool [smarter than one I could create] could "guess" about a structure for the data and reformat it as json. The key is getting the data into "some sort of json output", even if imperfect.
For example, taking an output like this:
$ ifconfig
eth0 Link encap:Ethernet HWaddr 06:41:73:c2:ca:15
inet addr:172.31.25.84 Bcast:172.31.31.255 Mask:255.255.240.0
inet6 addr: fe80::441:73ff:fec2:ca15/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2323065 errors:0 dropped:0 overruns:0 frame:0
TX packets:2334632 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:951649848 (951.6 MB) TX bytes:702385664 (702.3 MB)
and then running:
$ ifconfig | ttjsoni # "Try To JSON It"
{
"eth0": {
"Link": "encap:Ethernet HWaddr 06:41:73:c2:ca:15 ",
"inet": "addr:172.31.25.84 Bcast:172.31.31.255 Mask:255.255.240.0",
...
}
}
or
$ ifconfig | ttjsoni # "Try To JSON It"
{
"eth0": {
[
"Link encap:Ethernet HWaddr 06:41:73:c2:ca:15 ",
"inet addr:172.31.25.84 Bcast:172.31.31.255 Mask:255.255.240.0"
...
]
}
}
Perhaps ttjsoni has some sort of pluggable system so that authors could write parsers for apps depending on the output?
Or maybe some sort of aggressiveness setting the user could pass in. So that ifconfig command could be as simple as arrays of lines or as complex as deeply nested map's of data.
Again, to me the key is getting it into json in a repeatable way. Once it is json i can use 'jq' to parse arbitrary structures and get whatever is needed out:
$ ifconfig | ttjsoni | jq '.inet.addr[0]'
172.31.25.84
Any chance a tool like this, or similar to this, exists?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment