The mystical AWK utility

Awk is one of the most useful commands in Unix. Awk stands for the three programmers which created the utility, Aho, Weinberger, and Kernighan. Awk gives you the ability to take STDIN and apply programming logic to it. For example:

ls -l | awk ‘{ print $3 }’

The above command simply prints the directory content in extended format (ls -l), passes the STDOUT to STDIN for awk (awk ‘{ print $3 }’), which simply takes the 3rd column, separated by space, which happens to produce an output similar to the following:

root
root
root
root

If you need to refresh on STDIN, STDOUT, end STDERR, I wrote a two series article explaining the. While the awk utility is very powerful, I mainly use it for printing columnar output to filter STDIN. I use two versions, the one listed above, and a version which has a different separator other than space, which is the default.

Let’s examine the following command:

echo “first,second,third,fourth” | awk -F, ‘{ print $2 }’

The output produced by the above command is:

second

The command simply echoed “first,second,third,fourth” to STDOUT, which became STDIN for the awk command. The awk command tool STDIN separated by “,” (-F,), and printed the second column ($2).

Awk is a very complicated utility, it can also have scripts associated with it. While theoretically, a script could be useful, in practice, I simply use awk when I have a need for columnar manipulation.

I hope that the awk utility is now clearer, keep in mind that we just scratched the surface and awk has many, many more features. I just showed you the most common use I make of awk.

Leave a Reply