Would appreciate to know about the special char or wildcard in the following command:
/sbin/ifconfig eth0 | awk -F ' *|:' '/inet addr/{print $4}'
What does the code below mean?
awk -F ' *|:' '
Would appreciate to know about the special char or wildcard in the following command:
/sbin/ifconfig eth0 | awk -F ' *|:' '/inet addr/{print $4}'
What does the code below mean?
awk -F ' *|:' '
The -F flag (for awk) means to use the following extended regular expression as a Field separator Which means it treats any of the characters between the single quotes here: ' *|:'
as delimiters.
Awk will then print the 4th field that has a match with inet addr
.
For example: if this were the output for ifconfig eth0
:
eth0 Link encap:Ethernet HWaddr 09:00:12:90:e3:e5
inet addr:192.168.1.29 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe70:e3f5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:54071 errors:1 dropped:0 overruns:0 frame:0
TX packets:48515 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:22009423 (20.9 MiB) TX bytes:25690847 (24.5 MiB)
Interrupt:10 Base address:0xd020
It should print out
192.168.1.29
To break down the regular expression:
' *|:'
the
' *'
Means any number of spaces, and the remaining characters have no special meaning, so either of them can be a delimiter. The *
itself is not a delimiter, it just means any number of the preceding character, a space.
As you know, ip command
exists in /sbin
and /bin
, So if you want to run your script in non-root, You can use the following statement :
mohsen@debian:~$ ip addr show eth0 |grep inet |grep -v inet6 | awk '{print $2}' |awk -F/ '{print $1}'
192.168.1.4