2/1/10

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.

1 awk '{print $3}' $filename

2 # Prints field #3 of file $filename to stdout.

3

4 awk '{print $1 $5 $6}' $filename

5 # Prints fields #1, #5, and #6 of file $filename.


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.

1 { total += ${column_number} }

This adds the value of column_number to the running total of "total". Finally, to print "total", there is an END command block, executed after the script has processed all its input.


1 END { print total }


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

* From <http://www.faqs.org/docs/abs/HTML/awk.html#AWKREF>

No comments:

Quick HTTP to HTTPS - Apache2

There are several methods for redirecting your Apache-based website visitors who might type your servers URL using the plain (non-secure) HT...