Overview of awk

The primarily purpose of awk is to processing textual data to produce formatted reports

The awk extraction and reporting language is [[data_driven?]] and is primarily used for taking actions against textual data for the purpose of producing formatted reports.

Components of an awk program

A [[script?]] in awk may contain the following components:

The awk utility performs a cycle of pattern matching and performing associated actions

The awk utility takes each [[line?]] of input and compares with each pattern in the [[script?]]. If a pattern matches the [[input_line?]], awk takes the action associated with the pattern. If the line does not match the pattern, the awk utility takes no action. If more than one pattern matches the [[input_file?]], then the action associated with each pattern are carried out in the order in which they are defined in the awk script. When awk has completed its comparisons for the first line of the [[input_file?]], the cycle is repeated for each of the subsequent lines in of input, until all lines have been read.

The awk utility can perform mathematical operations

The awk utility can perform mathematical operations. This makes awk a useful tool for producing tallies and summaries:

It is possible to process multiple files using patterns and actions

Using awk, it is possible to search one or more files for specified pattern matches and performing associated actions.

The awk language supports common programming constructs

The awk language provides support for programming constructs such as [[loop?]]s and conditional branches, and enables arithmetic and string operations to be performed.

Where is awk on the hammer scale?

The awk utility is rated at 3 (a General Hammer) on the hammer scale, and is classified as "A general purpose package with various configuration options, some of which may not be needed."


Each line of an awk script has the following [[syntax?]]:

pattern { action }

The pattern is used to select lines from the [[input_file?]]. The awk utility performs the associated action on all lines that are selected by the pattern. The action is enclosed within braces so that the awk interpreter can differentiate it from the pattern.

If several patterns select the same line, awk takes the actions associated with each of the patterns in the order that they first appear within the [[program?]]. It is possible for awk to send a single line from the [[input_file?]] to its [[standard_output?]] more than once.


If a [[program_line?]] does not contain a pattern, the awk utility applies the action to all lines in the [[input_file?]].

If a [[program_line?]] does not contain an action, awk copies the selected lines to [[standard_output?]].