AWK : A Quick and Simple Tutorial

Duy-Ky Nguyen @ uTHC
I have to maintain several Linux boxes for several (embedded) e-Linux projects. I want to differentiate their terminals using their prompt with their IP, only the Host_ID, not the whole {Net_ID, Host_ID}, to keep the prompt short. So I need to extract the info from "ifconfig".

I found "Simple awk tutorial" the simplest I could find, but it's still complicated and not clear enough for enough understanding to use it effectively to get job done!

So, after some effort to dig in deep enough to do what I want, I'd like to share with you with my experience.

AWK takes action upon pattern matched and provides all fields $1, $2, $3, ..., $NF (last field: Number of Field) separated by delimiter blank ' ' or 'x' in "-Fx" on line by line from input file. AWK takes action on every line if no /pattern/ is provided

Basic syntax


awk '/pattern1/ {action1} /pattern2 {action2} . . .' file

cmd | awk '/pattern1/ {action1} /pattern2 {action2} . . .'


awk '{print}' file ==> print out every line in file

awk '{print $2}' file ==> print out field 4 delimited by ' ' for every line in file


awk '/100/ {print $3}' file ==> print out field 3 by ' ' if the line contains '100'

awk -F: '/100/ {print $4}' file ==> print out field 4 by ':' if the line contains '100'



Let "date" puts out "Sat Jun 13 01:06:45 PDT 2015"

Use blank ' ' as delimiter
date | awk '{print}' ==> "Sat Jun 13 01:06:45 PDT 2015", same as {print $0}, or simply "date"

date | awk '{print $2}' ==> "Jun"

date | awk '{print $4}' ==> "01:06:45"

date | awk '{print $NF}' ==> "2015"    # built-in "$NF" for Number of Field

Use ':' as delimiter
date | awk -F: '{print $1}' ==> "Sat Jun 13 01"

date | awk -F: '{print $2}' ==> "06"

date | awk -F: '{print $NF}' ==> "45 PDT 2015"



Let 'ifconfig eth0' give
eth0      Link encap:Ethernet  HWaddr 08:00:27:f2:12:7e  
          inet addr:10.0.0.9  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fef2:127e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:87 errors:0 dropped:0 overruns:0 frame:0
          TX packets:57 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:8350 (8.3 KB)  TX bytes:7713 (7.7 KB)



then
ipconfig eth0 | awk -F. '/inet / {print $0}' ==> "inet addr:10.0.0.9 Bcast:10.0.0.255 Mask:255.255.255.0" 
ipconfig eth0 | awk '/inet / {print $1}' ==> "inet addr:10" 

ipconfig eth0 | awk '/inet / {print $2}' ==> "0"

ipconfig eth0 | awk '/inet / {print $3}' ==> "0"

ipconfig eth0 | awk '/inet / {print $4}' ==> "9 Bcast:10"

ipconfig eth0 | awk '/inet / {print $4}' | awk '{print $1}' ==> "9"

Now use "printf" for formatting
ipconfig | awk '/inet / {printf "ip_%d", $4}' ==> "ip_9" 

For the script to be use for any eth?, let 'ifconfig' give

eth1      Link encap:Ethernet  HWaddr 08:00:27:f2:12:7e  
          inet addr:10.0.0.9  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fef2:127e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:75 errors:0 dropped:0 overruns:0 frame:0
          TX packets:57 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:7222 (7.2 KB)  TX bytes:7713 (7.7 KB)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:120 errors:0 dropped:0 overruns:0 frame:0
          TX packets:120 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:9864 (9.8 KB)  TX bytes:9864 (9.8 KB)


ipconfig | awk '/inet / {print $0}'
==>
"inet addr:10.0.0.9 Bcast:10.0.0.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0"

Note that there're 2 "inet", so use built-in "NR" (Number of Record/Row) to select
ipconfig | awk -F. '/inet / {if(NR==2) print $4}' ==> "inet addr:10.0.0.9 Bcast:10.0.0.255 Mask:255.255.255.0" 

ipconfig | awk -F. '/inet / {if(NR==2) print $4} | awk '{print $1}' ==> "9"

ipconfig | awk -F. '/inet / {if(NR==2) print $4} | awk '{printf "ip_%d", $1}' ==> "ip_9"