Awk
Introduction
Awk stands for aho, weinberger and kernighan, the respective names of its author. It is good for manipulating rows of text by allowing the script language to parse the input file well. It is often similar to perl which takes much from awks syntax.
Awk can do many things sed can do and more. The two go hand in hand for a script writing system administrator.
Awk and /etc/passwd
It's usually a better idea to use one command instead of using a pipe to another command, where possible. A common mistake:
# grep root /etc/passwd | awk -F: '{print $7}' /bin/bash
which can be easily done all in awk:
# awk -F: '/root/ {print $7}' /etc/passwd /bin/bash
Awk and ps
Say you wanted to find out how much resident memory xfce4 was using on your system, and it appears most xfce applications start with "xf":
$ ps auwx | awk '/xf/{print $5}' 15924 14668 11948 11764 12944 16264 1860
If you wanted to use awk to add the results together instead of doing it manually:
$ ps auwx | awk '/xf/{ tot += $5 } END { print tot }' 69108
N.B. This can be misleading in the case of programs that use large amounts of shared memory (like java).
Awk and multi-row documents
pretend you have a file that has IP numbers where multiple can be on one row and you need to flatten these into 1 per row here is how the solution looks like:
francisco$ cat ipfile 192.168.0.1 192.168.0.2 192.168.0.3 10.0.0.1 10.0.0.2 10.0.0.3
francisco$ awk '{ for (i = 1; i <= NF ; i++) print $i; }' ipfile 192.168.0.1 192.168.0.2 192.168.0.3 10.0.0.1 10.0.0.2 10.0.0.3