How cool is Linux AWK

Awk is geared toward text processing and report generation, yet features many well-designed features that allow for serious programming. And, unlike some languages, awk's syntax is familiar, and borrows some of the best parts of languages like C, python, and bash (although, technically, awk was created before both python and bash). Awk is one of those languages that, once learned, will become a key part of your strategic coding arsenal. Let's go ahead and start playing around with awk to see how it works. awk

The essential organization of an AWK program follows the form:
pattern { action }
The pattern specifies when the action is performed. Like most UNIX utilities, AWK is line oriented. That is, the pattern specifies a test that is performed with each line read as input. If the condition is true, then the action is taken. The default pattern is something that matches every line.
See the example here:
You should see the contents of your /etc/fstab file appear before your eyes.
awk '{ print }' /etc/fstab
LABEL=/     /           ext4    defaults,noatime  1   1
tmpfs       /dev/shm    tmpfs   defaults        0   0
devpts      /dev/pts    devpts  gid=5,mode=620  0   0
sysfs       /sys        sysfs   defaults        0   0
proc        /proc       proc    defaults        0   0
/mnt/myswapfile               swap                    swap    defaults        0 0
When we executed awk, it evaluated the print command for each line in /etc/fstab , in order. All output is sent to stdout, and we get a result identical to catting /etc/fstab. This time we will do the same thing but with a different awk syntax:
$ awk '{ print $0 }' /etc/fstab
LABEL=/     /           ext4    defaults,noatime  1   1
tmpfs       /dev/shm    tmpfs   defaults        0   0
devpts      /dev/pts    devpts  gid=5,mode=620  0   0
sysfs       /sys        sysfs   defaults        0   0
proc        /proc       proc    defaults        0   0
/mnt/myswapfile               swap                    swap    defaults        0 0
The $0 variable represents the entire current line, so print and print $0 do exactly the same thing. Next example will replace the lines with a string of your choice:
awk '{ print "aodba" }' /etc/fstab

aodba
aodba
aodba
aodba
aodba
aodba
aodba

Breaking Text

Awk is really good at handling text that has been broken into multiple logical fields, and allows you to effortlessly reference each individual field from inside your awk script. The following script will print out a list of all user accounts on your system:
 awk -F":" '{ print $1 }' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
rpc
ntp
saslauth
When we called awk, we use the -F option to specify ":" as the field separator. When awk processes the print $1command, it will print out the first field that appears on each line in the input file.

More strings

 awk -F":" '{ print $1 $3 }' /etc/passwd
root0
bin1
daemon2
adm3
lp4
sync5
shutdown6
halt7
mail8
uucp10
operator11
games12
As you can see, awk prints out the first and third fields of the /etc/passwd file, which happen to be the username and uid fields respectively.

Strings with spaces in between

awk -F":" '{ print $1 " " $3 }' /etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
uucp 10
operator 11
When you call print this way, it'll concatenate $1, " ", and $3, creating readable output.

Adding test to the combination

awk -F":" '{ print "username: " $1 "ttuid:" $3 }' /etc/passwd
username: root          uid:0
username: bin           uid:1
username: daemon                uid:2
username: adm           uid:3
username: lp            uid:4
username: sync          uid:5
username: shutdown              uid:6
username: halt          uid:7
This will become readable output concatenated with the extra text. This ends of first AWK tutorial.  See next tutorial on Using AWK Scripts