.TH awk "" "" Command
.PC "Pattern-scanning language"
\fBawk [ \fIPOSIX or GNU style options \fB] \-f \fIprogram-file \fB[ -- ] \fIfile ...\fR
\fBawk [ \fIPOSIX or GNU style options \fB] [ -- ] \fIprogram-text file ...
.PP
.B awk
is a general-purpose language designed for processing input data.
Its features allow you to write programs that
scan for patterns, produce reports,
and filter relevant information from a mass of input data.
It acts upon the contents of each
.IR program-file ,
or the standard input if no
.I -program-file
is specified.
.PP
You can specify the program either as an argument
(usually enclosed in quotation marks to prevent interpretation by the shell
.BR sh )
or in the form
.B \-f
.IR program-file .
If no
.B \-f
option appears, the first non-option argument is the
.B awk
program.
.PP
.B awk
views its input as a sequence of records,
each consisting of zero or more fields.
By default, newlines separate records and
white space (spaces or tabs) separates fields.
The option
.BI \-F c
changes the input field separator characters to the characters in the string
.IR c .
An
.B awk
program can also change the field and record separators.
The program can access the values of each field and the entire record through
built-in variables.
.PP
For details on the construction of
.B awk
programs, consult the tutorial to \fBawk\fR that appears in this manual.
Briefly, an
.B awk
program consists of one or more lines, each containing a
.I pattern
or an
.I action,
or both.
A
.I pattern
determines whether
.B awk
performs the associated
.I action.
It may consist of regular expressions, line ranges, boolean combinations of
variables, and beginning and end of input-text predicates.
If no
.I pattern
is specified,
.B awk
executes the
.I action
(the pattern matches by default).
.PP
An
.I action
is enclosed in braces.
The syntax of actions is C-like, and consists of simple and compound
statements constructed from constants (numbers, strings), input fields,
built-in and user-defined variables, and built-in functions.
If an
.I action
is missing,
.B awk
prints the entire input record (line).
.PP
Unlike
.B lex
or
.BR yacc ,
.B awk
does not compile programs into an executable image,
but interprets them directly.
Thus,
.B awk
is ideal for quickly-implemented, one-shot efforts.
.SH Examples
The following examples illustrate the economy of expression of
.B awk
programs.
.PP
The first example reads the standad input, and
echoes all lines containing the string ``\*(CO'':
.DM
	awk '/COHERENT/'
.DE
.PP
To exit, type \fB<ctrl-D>\fR/
.PP
The built-in variable
.B NR
is the number of the current input record.
The next example reads the standard input, and prints the number of
records you typed after you exit (again, by typing \fB<ctrl-D>\fR):
.DM
	awk 'END { print NR }'
.DE
.PP
The built-in variable
.B $3
gives the value of the third field of the current record.
The last example sums the third field from each record you type on the
standard input, and prints the total when you exit:
.DM
	awk '{ sum += $3 }
		END	{ print sum }'
.DE
.SH "See Also"
.Xr commands, commands
.Xr gawk, gawk
.Xr lex, lex
.Xr sed, sed
.Xr yacc yacc
.br
\fIIntroduction to the awk Language,\fR tutorial.
.SH Notes
Beginning with release 4.2.14 of \*(CO,
.B awk
has been replaced by
.BR gawk ,
the GNU implementation of this language.
For details on this implementation of the
.B awk
language, see the Lexicon entry for
.BR gawk .
