

awk                          Command                          awk




Pattern-scanning language

aawwkk [-yy][-FF _c][-ff _p_r_o_g_f_i_l_e][_p_r_o_g][_f_i_l_e ...]

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 on  each input file  following the
commands you  write into an awk program.   awk reads the standard
input if no file is specified, which allows it to act as a filter
in a pipeline; the file name `-' means the standard input.

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

The  option  flag -y  specifies  that  any lower-case  alphabetic
character in  a regular expression pattern  should match both it-
self and the  corresponding upper-case letter.  This is identical
to the  matching found in the  pattern-matching program grep with
the -y option.

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  aawwkk 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 execut-
able 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


COHERENT Lexicon                                           Page 1




awk                          Command                          awk



awk programs.

The  first  example   prints  all  lines  containing  the  string
``COHERENT'' (identical to ggrreepp CCOOHHEERREENNTT):


        /COHERENT/


The  built-in variable  NR  is the  number of  the current  input
record,  so the  following program prints  the number  of records
(lines) in the input stream.


        END     { print NR }


The built-in  variable $3 gives  the value of the  third field of
the current record, so the following program sums the elements in
column three of an input table and prints the total:


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


***** See Also *****

commands, grep, lex, sed, yacc
_I_n_t_r_o_d_u_c_t_i_o_n _t_o _t_h_e _a_w_k _L_a_n_g_u_a_g_e

***** Notes *****

There is  no way to  have a null  field, such as  is necessary to
describe the colon-separated fields in /etc/passwd.

awk converts  between strings and  numbers automatically.  Adding
zero  to a  string  forces awk  to  treat it  as  a number;  con-
catenating "" to a number forces awk to treat it as a string.


















COHERENT Lexicon                                           Page 2


