I don’t have a pun title for this one.

The awk command is used to transform stream of text (like sed). It is likely named after it’s creators Aho, Weinberger and Kerninghan. The command has its own scripting language called AWK.

An AWK program is a series of pattern command pairs.

pattern { command }
pattern { command }
...

Patterns can be regular expressions (see a pattern here?).

A simple AWK program looks like this:

ps aux | awk "/bash/ {print}"

It matches a pattern and prints the line. Pretty much the same as ps aux | grep bash.

AWK understand columns, so we can select just a column.

ps aux | awk '/bash/ { print $2 }'

Or multiple columns:

ps aux | awk '/bash/ { print $1, $2 }'

We can set the field separator (FS):

cat /etc/passwd | awk '/bash/ { print $1, $7 }' FS=':'

We can sum columns:

ps au | awk '{ SUM+=$6 } END { print SUM }'

Variable SUM is assigned to the sum of column 6, then we print the Variable.

We can also skip rows (aka records).

ps au | awk 'FNR>1 { print }'

Here we are skipping the first row. FNR (for File Number of Records) basically means the line number.

We can count number of characters in a file.

First create a test file:

python -c 'import this' > zen.txt

Then count the characters.

awk '{ chars += length + 1 } END { print chars }' zen.txt

This is similar to wc -m zen.txt. The + 1 is to account for newlines. END is a pattern that matches the end of a file. There is also a BEGIN.

Since we can do arithmetic in AWK, we can make a script to count the number of normal pages.

awk '{ chars += length + 1 } END { print chars / 2400 }' zen.txt

AWK also supports functions.

awk 'function normal_pages(characters) {
  return characters / 2400
}
{
  chars += length + 1
}
END {
  print normal_pages(chars)
}' zen.txt

You can see more examples on Wikipedia or read the full User’s Guide for GNU Awk.