AWK programming lesson 3

So we've covered awk's general syntax . Now let's start getting into the funky stuff.

awk has "special" match strings: "BEGIN" and "END"

The BEGIN directive gets called once before any data line is read, and never again.
The END directive gets called after all lines have been read. If multiple files are given, it only gets called after the very last file has been completed.

Generally, you would use the BEGIN bit to initialize things, and the END bit for summaries, or cleanup.

Example:


BEGIN    	{ maxerrors=3 ; logfile=/var/log/something ; tmpfile=/tmp/blah}
  ...		{ blah blah blah }
/^header/	{ headercount += 1 }
END		{ printf("total headers encountered=%d\n", headercount);

This will count the number of times "header" appears in an input file,and print out the total only after processing the entire file.

AWK also has lots of other special variables, that you can use in the { } section. For example,


print  NF

will give you the total number of columns (Number of Fields) in the current line. FILENAME will be the current filename, assuming the filename was given to awk, and you're not piping to it.

You CANNOT CHANGE NF yourself.

Similarly, there is a NR variable, that tells you how many lines you have processed. ("Number of Records")

There are other special variables, and even ones you CAN change in the middle of the program. See your local manpage for more details.


Author: [email protected]
Top of AWK lessons - Next: Chapter 4 Prev: Chapter 2
bolthole main page