Reputation: 121
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
Upvotes: 2
Views: 279
Reputation: 1
I found this command to be more helpful to filter unwanted stuff from the ifconfig command:
/sbin/ifconfig/ eth0 | grep 'inet' | cut -d: -f2 |awk '{print $2}'
eth0 - to get eth0 ip details inet - shows only strings containing inet cut -d: this will convert all inet containing string into a list -f2 to access 2nd string/line awk '{print $2}' to get the desire output where $2- for string index at 2nd position
Upvotes: 0
Reputation: 14958
ifconfig | awk '/^lo/,/\n\n/{ e=1 }!e && $1!~/^(RX|TX)$/{ print $1,$1~/:/?"":$2}'
The /^lo/,/\n\n/
will set e
to 1 for the loopback part.
We only print something if e
equals 0 and
if the first element does not start with an R
or T
.
Finally, only print $2
is $1
does not contain a :
Upvotes: 0
Reputation: 204310
$ cat tst.awk
/^[[:alnum:]]/ {
f = ($1 == "lo:" ? 0 : 1)
if (f) {
print sep $1
sep = ORS
}
}
f && ($1 ~ /^(ether|inet)$/) {
print $1, $2
}
$ awk -f tst.awk file
enp2s0f1:
ether xx:xx:xx:xx:xx:xx
wlp3s0:
inet xxx.xxx.xxx.xxx
ether xx:xx:xx:xx:xx:xx
Upvotes: 1
Reputation: 785761
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
}'
Upvotes: 4