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:
This will count the number of times "header" appears in an input file,and print out the total only after processing the entire file.BEGIN { maxerrors=3 ; logfile=/var/log/something ; tmpfile=/tmp/blah} ... { blah blah blah } /^header/ { headercount += 1 } END { printf("total headers encountered=%d\n", headercount);
AWK also has lots of other special variables, that you can use in the { } section. For example,
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.print NF
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.