1

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 ' *|:' '  
1
  • Did I fully explain that? If so, you can accept my answer by clicking the checkmark next to it, which will give you plus 2 to your reputation. Welcome to the site, by the way. Plus 1 for the interesting question. – Aaron Hall May 14 '15 at 4:43
2

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.

1
  • @ Aaron .. for yeilding the same output i used the below code.. " ifconfig | grep 'inet addr' | awk {'print $2'} | sed s/.*://" What does sed s/.*:// means ? i learned it somwhere to clear the matching pattern but did not get the holistic meaning .... -> basically ".*:" – krock1516 May 14 '15 at 6:12
0

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
1
  • That's correct.. – krock1516 May 15 '15 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.