How to display only few specified columns from first row in each block, i.e. formatting ifconfig output?

Viewed 42

There are several network interfaces and while listing all of them with ifconfig, I'd like to keep just the interface name and MTU.

In other words, how to transform something like this:

$ ifconfig
cali0a56gaa5a33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 8122
        inet6 fe80::ecee:eeff:feee:eeee  prefixlen 64  scopeid 0x20<link>
        ether ee:ee:ee:ee:ee:ee  txqueuelen 0  (Ethernet)
        RX packets 12890  bytes 129366676 (129.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16107  bytes 129601562 (129.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

cali4edac9c332d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 8122
        inet6 fe80::ecee:eeff:feee:eeee  prefixlen 64  scopeid 0x20<link>
        ether ee:ee:ee:ee:ee:ee  txqueuelen 0  (Ethernet)
        RX packets 111555  bytes 9806510 (9.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 148838  bytes 151003850 (151.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

into this:

cali0a56gaa5a33: mtu 8122
cali4edac9c332d: mtu 8122

The command $ ifconfig | awk '{ print $1 " " $3 " " $4 }' does the job partially: MTU gets displayed, but all indented rows appear as well.

2 Answers

You may use this awk:

ifconfig | awk 'NF > 1 && $(NF-1) == "mtu" {print $1, $(NF-1), $NF}'

cali0a56gaa5a33: mtu 8122
cali4edac9c332d: mtu 8122
  • Condition NF > 1 && $(NF-1) == "mtu" makes sure if there are more than 1 field and last-1 field value is mtu
  • print $1, $(NF-1), $NF prints first, last-1, last fields

I would use GNU AWK following way for this task, let ifconfig output be

cali0a56gaa5a33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 8122
        inet6 fe80::ecee:eeff:feee:eeee  prefixlen 64  scopeid 0x20<link>
        ether ee:ee:ee:ee:ee:ee  txqueuelen 0  (Ethernet)
        RX packets 12890  bytes 129366676 (129.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16107  bytes 129601562 (129.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

cali4edac9c332d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 8122
        inet6 fe80::ecee:eeff:feee:eeee  prefixlen 64  scopeid 0x20<link>
        ether ee:ee:ee:ee:ee:ee  txqueuelen 0  (Ethernet)
        RX packets 111555  bytes 9806510 (9.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 148838  bytes 151003850 (151.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

then

ifconfig | awk '/^[[:alnum:]]/{print $1,$3,$4}'

output

cali0a56gaa5a33: mtu 8122
cali4edac9c332d: mtu 8122

Explanation: I do print selected fields only for lines which does starts with alphanumeric character, thus excluding indented (do start with whitespace) and empty lines. Note that I use , rather than concatenating with single spaces, which prompt GNU AWK to concate using OFS (output field separator) which by default is space. This allow easy change in future, for example if need would arise to use tab rather than spaces it is sufficent to add BEGIN{OFS="\t"} rather than change every space.

(tested in gawk 4.2.1)

Related