B.2. Awk

Awk

Awk is a full-featured text processing language with a syntax reminiscent of C. While it possesses an extensive set of operators and capabilities, we will cover only a couple of these here - the ones most useful for shell scripting.

Awk breaks each line of input passed to it into fields. By default, a field is a string of consecutive characters separated by whitespace, though there are options for changing the delimiter. Awk parses and operates on each separate field. This makes awk ideal for handling structured text files, especially tables, data organized into consistent chunks, such as rows and columns.

Strong quoting (single quotes) and curly brackets enclose segments of awk code within a shell script.

awk '{print $3}'
# Prints field #3 to stdout.

awk '{print $1 $5 $6}'
# Prints fields #1, #5, and #6.

We have just seen the awk print command in action. The only other feature of awk we need to deal with here is variables. Awk handles variables similarly to shell scripts, though a bit more flexibly.

{ total += ${column_number} }
This adds the value of column_number to the running total of "total". Finally, to print "total", there needs to be an END command to terminate the processing.
END { print total }

Corresponding to the END, there is a BEGIN, for a code block to be performed before awk starts processing its input.

For examples of awk within shell scripts, see:

  1. Example 11-8

  2. Example 16-5

  3. Example 12-23

  4. Example 34-3

  5. Example 9-17

  6. Example 11-12

  7. Example 28-1

  8. Example 28-2

  9. Example 10-3

  10. Example 12-31

  11. Example 9-20

  12. Example 12-3

That's all the awk we'll cover here, folks, but there's lots more to learn. See the appropriate references in the Bibliography.