Pretty printing terminal output into JSON/CSV
There is a command called column
which allows you to format the lines produced by a command into symmetrical rows/columns, which can then be easily converted into CSV or JSON.
This will be quite handy for reading and populating files. In this case, I needed to populate /etc/ethers/
from the arp table
More info on column
Note: This is based on the column command in fedora linux, it may be different in other distros
Creating a table
This bit is simple enough, we delete unneeded fields with awk, and then use the command column -t
to turn it into a symmetrical table.
ip -4 neighbor show nud FAILED \
| awk '{print $1,"\t",$5,"\t",$3}' \
| column -table
172.17.0.2 02:42:ac:11:ba:32 docker0
172.17.29.71 52:54:00:85:be:17 docker0
192.168.0.12 52:54:00:24:fe:41 virbr2
192.168.1.19 b8:27:eb:15:ed:18 enp3s0
192.168.1.1 44:d9:e7:9e:de:52 enp3s0
10.53.2.53 b8:27:eb:15:ad:98 enp3s0
172.17.65.56 52:54:00:74:be:21 docker0
192.168.122.229 52:54:00:24:ff:68 virbr0
Adding identifiers to the fields
column
is a powerful command for making tables, lets specify some field names for the table.
We do this by adding the argument --table-columns
to `column' and giving a comma seperated list of field names e.g.
column --table --table-columns "IPv4Address,MacAddress,InterfaceName,Comment" --json
We can even get JSON!
{
"table": [
{
"ipv4address": "172.17.0.2",
"macaddress": "02:42:ac:11:ba:32",
"interfacename": "docker0",
"comment": null
},
{
"ipv4address": "172.17.29.71",
"macaddress": "52:54:00:85:be:17",
"interfacename": "docker0",
"comment": null
},
{
"ipv4address": "192.168.0.12",
"macaddress": "52:54:00:24:fe:41",
"interfacename": "virbr2",
"comment": null
},
{
"ipv4address": "192.168.1.19",
"macaddress": "b8:27:eb:15:ed:18",
"interfacename": "enp3s0",
"comment": null
},
{
"ipv4address": "192.168.1.1",
"macaddress": "44:d9:e7:9e:de:52",
"interfacename": "enp3s0",
"comment": null
},
{
"ipv4address": "10.53.2.53",
"macaddress": "b8:27:eb:15:ad:98",
"interfacename": "enp3s0",
"comment": null
},
{
"ipv4address": "172.17.65.56",
"macaddress": "52:54:00:74:be:21",
"interfacename": "docker0",
"comment": null
},
{
"ipv4address": "192.168.122.229",
"macaddress": "52:54:00:24:ff:68",
"interfacename": "virbr0",
"comment": null
}
]
}