Basic structure of a command.

pattern { actions }

pattern {

}


pattern is optional and identifies targeted line or lines to take action on.

A pattern without an action becomes a grep.

pattern may :

BEGIN - identifies commands to run before processing data file. Useful for printing header info and setting an alternative field separator.

BEGIN { FS = ":"; print "Today's report" }

END - identifies commands to run after all records processed. Useful for printing totals and doing calculations on accumulations.


actions specify actions to take on each line. If no pattern match provided, awk will attempt to act on all lines.

Actions are enclosed in { }, braces.

Individual actions are separated by ;, semi-colons or line breaks.

There may be multiple sets of braces with or without pattern filters.

Action commands are modeled after the C language. But there are a few simplified commands also available.

{ print $1, $2 }
prints the 1st and second fields of all records. The comma indicates that the fields will be separated by the standard OFS (output field separator).