COHERENT manpages

This page displays the COHERENT manpage for awk [Pattern-scanning language].

List of available manpages
Index


awk -- Command

Pattern-scanning language
awk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
awk [ POSIX or GNU style options ] [ -- ] program-text file ...

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  program-file, or the standard input if no -
program-file is specified.

You  can specify  the program  either as an  argument (usually  enclosed in
quotation marks to prevent interpretation by the shell sh) or in the form -
f program-file.  If no -f option appears, the  first non-option argument is
the awk program.

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  -Fc changes  the  input  field
separator characters to the characters in  the string c. An 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.

For details  on the construction  of awk programs, consult  the tutorial to
awk that appears  in this manual.  Briefly, an awk  program consists of one
or more lines, each containing a  pattern or an action, or both.  A pattern
determines whether  awk performs the  associated action. It  may consist of
regular expressions,  line ranges,  boolean combinations of  variables, and
beginning and  end of input-text  predicates.  If no  pattern is specified,
awk executes the action (the pattern matches by default).

An 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 action is missing, awk  prints the entire input
record (line).

Unlike lex or yacc, awk does not compile programs into an executable image,
but interprets them  directly.  Thus, awk is ideal for quickly-implemented,
one-shot efforts.

Examples

The  following  examples  illustrate  the  economy  of  expression  of  awk
programs.

The first example reads the  standad input, and echoes all lines containing
the string ``COHERENT'':

    awk '/COHERENT/'

To exit, type <ctrl-D>/

The built-in  variable 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 <ctrl-D>):

    awk 'END { print NR }'

The built-in variable $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:

    awk '{ sum += $3 }
        END { print sum }'

See Also

commands,
gawk,
lex,
sed,
yacc
Introduction to the awk Language, tutorial.

Notes

Beginning with  release 4.2.14 of COHERENT, awk has  been replaced by gawk,
the   GNU  implementation   of  this   language.    For  details   on  this
implementation of the awk language, see the Lexicon entry for gawk.