I’d like to pretty print the ip addresses and show the output in in tabular format, including all the meta data such as valid_lft, temporary, etc.
I figured out that ip -j addr show eth0
is giving me the JSON I need. If I run ip -j addr show eth0 | jq -r '.[0].addr_info'
the data is already filtered for what I’m interested in.
I know that I can use @tsv
to get a table format and to accommodate for different value lengths I can pipe to column -ts $'t'
.
What I cannot figure out is how I can iterate through all the objects and extract the keys first, because if I don’t do that, the output values will be in incorrect columns, based on what keys each of the object has (or rather not has).
I already succeeded in extracted the keys using
ip -j addr show eth0 | jq -r '[.[0].addr_info | .[] | keys_unsorted[]] | reduce .[] as $a ([]; if IN(.[]; $a) then . else . += [$a] end)'
Now I do not know how to combine all of this.
Essentially, the following is yielding the desired result, but I do not like it, because the headers/keys are manually defined:
ip -j addr show eth0 | jq -r '(["Family", "Local", "Prefixlen", "Broadcast", "Scope", "Dynamic", "Noprefixroute", "Label", "Valid_Life_Time", "Preferred_Life_Time", "Temporary", "Deprecated", "Mngtmpaddr"] | (., map(length*"-"))), (.[0].addr_info | .[] | [ .family, .local, .prefixlen, .broadcast, .scope, .dynamic, .noprefixroute, .label, .valid_life_time, .preferred_life_time, .temporary, .deprecated, .mngtmpaddr ] | map(.//"-")) | @tsv' | column -ts $'t'
Any help is much appreciated.