I'm new to the whole awk tool, it's seems really complicated to me.
Sample ifconfig output:
enp2s0f1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 338 bytes 27536 (26.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 338 bytes 27536 (26.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.x broadcast xxx.xxx.xxx.xxx
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 143296 bytes 172270775 (164.2 MiB)
RX errors 0 dropped 5 overruns 0 frame 0
TX packets 32786 bytes 6774762 (6.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
So far I got a command for ifconfig filter that throws away loopback as well as other unwanted stuff.
ifconfig | awk 'BEGIN{ORS=RS="\n\n"} !/^lo/{print}' | awk '{print $1, $2}' | awk '!/^RX/{print}' | awk '!/^TX/{print}'
And here's an output:
enp2s0f1: flags=4099<UP,BROADCAST,MULTICAST>
ether xx:xx:xx:xx:xx:xx
wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
inet xxx.xxx.xxx.xxx
inet6 xxxx::xxxx:xxxx:xxxx:xxxx
ether xx:xx:xx:xx:xx:xx
I researched similar problems, but I want to know how to cut from middle of the line using awk
I already tried a line like:
awk '!/flags=/{print}'
but it removes whole line that include flags
Desired output:
enp2s0f1:
ether xx:xx:xx:xx:xx:xx
wlp3s0:
inet xxx.xxx.xxx.xxx
ether xx:xx:xx:xx:xx:xx
You may try this awk
:
ifconfig |
awk '
/^[[:alnum:]]+: / { # if we encounter an interface name
n = $1 # save name in variable n
p = 1 # set flag p=1
}
# interface does not start with "lo" and protocols are inet or ether
n !~ /^lo/ && /^[[:blank:]]+(inet|ether) / {
if (p) { # if flag is set
print n # print interface name
p = 0 # reset flag p=0
}
print $1, $2 # print protocol and address
}'