Skip to main content

Timeline for jq - ip addr show in tabular format

Current License: CC BY-SA 4.0

11 events
when toggle format what by license comment
Sep 11, 2024 at 8:18 comment added hanjo Even shorter: ip -j a | jq '.[] | {"ifname"} as $ifname | .addr_info[] | $ifname + .' | mlr --j2p --barred unsparsify
Sep 11, 2024 at 8:12 comment added hanjo I figured out I can further simplify it by using * instead of map to get ifname in front: ip -j a | jq '.[] | .ifname as $ifname | .addr_info | {ifname: $ifname} * .[]' | mlr --j2p --barred unsparsify
Sep 11, 2024 at 7:52 comment added Kusalananda @hanjo Well, :-), I hope the millisecond performance gain makes up for the time spent working on the script...
Sep 11, 2024 at 7:49 comment added hanjo @Kusalananda good to know, thanks. Still learning here. On my test machine (Raspberry Pi Model B Rev 2) avoiding the foreach saved 3ms, so I guess the performance advantage is neglectable for this specific use case, but I get the idea. Don't use loops if not absolutely required.
Sep 11, 2024 at 7:40 comment added Kusalananda @hanjo Don't do explicit counting loop when you don't need to: .[] | .ifname as $ifname | .addr_info | map(.ifname=$ifname) | flatten. If you want the ifname field first in the result, then reorder with mlr: mlr --j2p unsparsify then reorder -f ifname
Sep 11, 2024 at 7:29 comment added hanjo Thank you, this is elegant. Since I asked to avoid any additional tools, I've accepted @StéphaneChazelas suggestion as answer below. However, I think I will be using this one regardless :-) On a side note, I thought it would be good to add the interface to the output, which can be achieved by using ip -j a | jq 'foreach .[] as $item (0; . +1; $item.addr_info [] as $a | {ifname: $item.ifname} * $a)' | mlr --j2p unsparsify
Sep 10, 2024 at 20:54 history edited Kusalananda CC BY-SA 4.0
added 6 characters in body
Sep 10, 2024 at 19:49 comment added terdon Ah, nice! I was hoping someone could come up with something better than that monstrosity I posted. You could just pass the output through perl -pe 's/([a-zA-Z]+)/\u$1/g; to get the capitalization as the OP had it.
Sep 10, 2024 at 19:48 history edited terdon CC BY-SA 4.0
On my system, at least, I need --barred
Sep 10, 2024 at 18:28 history edited Kusalananda CC BY-SA 4.0
added 96 characters in body
Sep 10, 2024 at 18:22 history answered Kusalananda CC BY-SA 4.0