On a RHEL 6.6 system, using ifconfig and GNU sed, I want to display only the Ethernet interfaces which aren't logical sub interfaces, or the loopback.
For example, the output should not contain interface records where the interface name is like eth0:134 or lo.
My approach so far has been to use sed with two expressions, The first, /eth[0-9]:/
to match on and include all lines containing 'ethN:, including every line after until a blank line is encountered, and delete, and a second expression to match on, /lo/ and all lines after until a blank line, and delete them as well.
For example:
[user@system ~]$ ifconfig -a | sed '/eth[0-9]:/,/^$/d; /lo/,/^$/d'
eth0 Link encap:Ethernet HWaddr 00:11:22:33:44:55
inet addr:192.168.0.50 Bcast: 192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1024 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
TX packets:2048 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
collisions:0 txqueuelen:1000
RX bytes:6455319 (6.1 MiB) TX bytes: 258478 (252.4 KiB)
Un-desired output looks like:
eth0:146 Link encap:Ethernet HWaddr 00:11:22:33:44:55
inet addr:192.168.0.51 Bcast: 192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:147 Link encap:Ethernet HWaddr 00:11:22:33:44:55
inet addr:192.168.0.52 Bcast: 192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric
eth0:148 Link encap:Ethernet HWaddr 00:11:22:33:44:55
inet addr:192.168.0.53 Bcast: 192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric
lo Link encap:Local Lookback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:605 errors:0 dropped:0 overruns:0 frame:0
TX packets:605 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:59008 (57.6 KiB) TX bytes:59008 (57.6 KiB)
I like this method of deleting all lines of output starting at and including the matched line until a blank line (^$) is encountered because there are a variable number of extra lines after the line containing the interface name. Either 2, additional, or 6 additional lines in this case.
This method allows there to be N additional lines of output as long as a blank line is still used as a separator between displayed interface records.
How can the second expression, /lo/,/^$/d'
be combined with the first?
Perhaps another approach to how the lines are matched (or not matched) is better?
Another issue is that this only matches the first 10 interfaces. There aren't more than 10, but it would be good to account for that in case there are.
I'd like to match on the first 100 interfaces with something like:
^[1-9][0-9]?$|^100$
Solutions using awk are ok as well.