AWK Reference Card
----------------------------------------------------------------------------
(Very similar to sed. See "sed" for list of regular expressions.)
Patterns of use:
awk 'commands' file
awk 'commands' file > newfile
awk -f command.file file > newfile
The commands take on the following form:
BEGIN { commands }
{ commands }
END { commands }
Each section should be enclosed in braces, but doesn't have to be if the BEGIN
and END sections are omitted.
BEGIN is preprocessing, before the first line of the file is read, while END
is postprocessing, after the last line has been read. The middle section is
done for every line in the file.
Every line is automatically parsed, i.e. broken into "words" and these words
are labeled as $1, $2, $3, etc. A word is any string of non-blank characters
between blanks. $0 is the entire line.
To cause a match on a character string, use / /, as in
awk '/fleece/{print}' somefile
This only matches lines which contain "fleece", and it prints them out. The
default action is print, so the following is equivalent:
awk '/fleece/' somefile
You can use C variables and syntax: if and while statements, printf, ++, etc.
There are some shortcuts, too, so for example "print" is simpler than print.
Also, the $0, $1, $2 variables are not C variables.
A couple of other standard variables are declared and set in AWK:
NR -- the number of the record (line) currently being processed
NF -- the number of fields in the current record
FS -- field separator, normally a blank, what the parser uses
... and others...
A couple of other things are unlike C, like the string matching operator,
and even the presence of regular expressions:
if (j ~ /Unix/) ...
The tilde (~) matches the string in variable "j" to the pattern in slashes.
This pattern may use regular expressions, like
j ~ /^Unix * .$/
which matches j only if it starts with "Unix " and ends the line with a
period. The asterisk in between stands for 0 or more characters.
For more examples, see the awk examples file.