Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

grep(1)

sed(1)

lex(1)

printf(3)



  nawk(1)                             CLIX                             nawk(1)



  NAME

    nawk - Runs a pattern scanning and processing language

  SYNOPSIS

    nawk [-F re] [parameter ... ] ['prog'] [-f progfile] [file ... ]

  FLAGS

    -F re         Defines the input field separator to be the regular
                  expression.  re

    -f progfile   Stores pattern-action statements.

  DESCRIPTION

    The nawk command is a new version of awk that provides capabilities
    unavailable in previous versions.  This version will become the default
    version of awk in the next major UNIX system release.

    The nawk command may be passed parameters, in the form x=... y=... , where
    x and y are nawk built-in variables (see the list following).

    The nawk command scans each input file for lines that match any of a set
    of patterns specified in prog.  The prog string must be enclosed in single
    quotes (') to protect it from the shell.  For each pattern in prog there
    may be an associated action performed when a line of a file matches the
    pattern.  The set of pattern-action statements may appear literally as
    prog or in a file specified with the -f progfile flag.

    Input files are read in order; if there are no files, stdin is read.  The
    filename - means stdin.  Each input line is matched against the pattern
    portion of every pattern-action statement; the associated action is
    performed for each matched pattern.

    An input line is normally made up of fields separated by white space.
    (This default can be changed by using the FS built-in variable or the -F
    re flag.)  The fields are denoted $1, $2, ... ; $0 refers to the entire
    line.

    A pattern-action statement has the following form:

    pattern { action }

    Either pattern or action may be omitted.  If there is no action with a
    pattern, the matching line is displayed.  If there is no pattern with an
    action, the action is performed on every input line.

    Patterns are regular expressions or arbitrary Boolean combinations (!, ||,
    &&, and parentheses) of relational expressions.  A relational expression



  2/94 - Intergraph Corporation                                              1






  nawk(1)                             CLIX                             nawk(1)



    is one of the following:

    expression relop expression
    expression matchop regular expression

    where a relop is any of the six relational operators in C, and a matchop
    is either ~ (contains) or !~ (does not contain).  A conditional is an
    arithmetic expression, a relational expression, the special expression

    var in array

    or a Boolean combination of these.

    The special patterns BEGIN and END may be used to capture control before
    the first input line has been read and after the last input line has been
    read, respectively.

    Regular expressions are as in egrep (see grep).  In patterns they must be
    surrounded by slashes.  Isolated regular expressions in a pattern apply to
    the entire line.  Regular expressions may also occur in relational
    expressions.  A pattern may consist of two patterns separated by a comma;
    in this case, the action is performed for all lines between an occurrence
    of the first pattern and the next occurrence of the second pattern.

    A regular expression may be used to separate fields by using the -F re
    flag or by assigning the expression to the built-in variable FS.  The
    default is to ignore leading blanks and to separate fields by blanks
    and/or tab characters.  However, if FS is assigned a value, leading blanks
    are no longer ignored.

    Other built-in variables include:

    ARGC       Command line argument count.

    ARGV       Command line argument array.

    FILENAME   Name of the current input file.

    FNR        Ordinal number of the current record in the current file.

    FS         Input field separator regular expression (default blank).

    NF         Number of fields in the current record.

    NR         Ordinal number of the current record.

    OFMT       Output format for numbers (default %.6g).

    OFS        Output field separator (default blank).

    ORS        Output record separator (default newline).



  2                                              Intergraph Corporation - 2/94






  nawk(1)                             CLIX                             nawk(1)



    RS         Input record separator (default newline).

    An action is a sequence of statements.  A statement may be one of the
    following:

    ⊕  if (conditional) statement [else statement]

    ⊕  while (conditional) statement

    ⊕  do statement while (conditional)

    ⊕  for (expression; conditional; expression) statement

    ⊕  for (var in array) statement

    ⊕  delete array[subscript]

    ⊕  break

    ⊕  continue

    ⊕  { [statement] ... }

    ⊕  expression     # commonly variable = expression

    ⊕  print [expression-list] [>expression]

    ⊕  printf format [, expression-list] [>expression]

    ⊕  next     # skip remaining patterns on this input line

    ⊕  exit [expr]     # skip the rest of the input; exit status is expr

    ⊕  return [expr]

    Statements are terminated by semicolons, newlines, or right braces.  An
    empty expression-list stands for the whole input line.  Expressions take
    on string or numeric values as appropriate, and are built using the
    operators +, -, *, / %, and concatenation (indicated by a blank).  The C
    operators ++, --, +=, -=, *=, /=, and %= are also available in
    expressions.  Variables may be scalars, array elements (denoted x[i]), or
    fields.  Variables are initialized to the null string or zero.  Array
    subscripts may be any string, not necessarily numeric; this allows for a
    form of associative memory.  String constants are quoted (").

    The print statement displays its arguments on stdout, or on a file if
    >expression is present, or on a pipe if | cmd is present.  The arguments
    are separated by the current output field separator and terminated by the
    output record separator.  The printf statement formats its expression list
    according to the format (see printf()).




  2/94 - Intergraph Corporation                                              3






  nawk(1)                             CLIX                             nawk(1)



    The nawk command has a variety of built-in functions:  arithmetic, string,
    input/output, and general.

    The arithmetic functions are: atan2, cos, exp, int, log, rand, sin, sqrt,
    and srand.  int truncates its argument to an integer.  rand returns a
    random number between 0 and 1.  srand(expr) sets the seed value for rand
    to expr or uses the time of day if expr is omitted.

    The string functions are as follows:

    gsub(for, repl, in)
           Behaves like sub (see below), except that it replaces successive
           occurrences of the regular expression (like the ed global
           substitute command).

    index(s, t)
           Returns the position in string s where string t first occurs, or 0
           if it does not occur at all.

    length(s)
           Returns the length of its argument taken as a string, or of the
           whole line if there is no argument.

    match(s, re)
           Returns the position in string s where the regular expression re
           occurs, or 0 if it does not occur at all.  RSTART is set to the
           starting position (which is the same as the returned value), and
           RLENGTH is set to the length of the matched string.

    split(s, a, fs)
           Splits the string s into array elements a[1], a[2], a[n], and
           returns n.  The separation is done with the regular expression fs
           or with the field separator FS if fs is not given.

    sprintf(fmt, expr, expr, ... )
           Formats the expressions according to the printf() format given by
           fmt and returns the resulting string.

    sub(for, repl, in)
           Substitutes the string repl in place of the first instance of the
           regular expression for in string in and returns the number of
           substitutions.  If in is omitted, nawk substitutes in the current
           record ($0).

    substr(s, m, n)
           Returns the n-character substring of s that begins at position m.

    The input/output and general functions are as follws:

    close(filename)
           Closes the file or pipe named filename.



  4                                              Intergraph Corporation - 2/94






  nawk(1)                             CLIX                             nawk(1)



    cmd getline
           Pipes the output of cmd into getline each successive call to
           returns the next line of output from cmd

    getline
           Sets $0 to the next input record from the current input file.

    getline <file
           Sets $0 to the next record from file.

    getline var
           Sets variable var instead.

    getline var <file
           Sets var from the next record of file.

    system (cmd)
           Executes cmd and returns its exit status.

    All forms of getline return 1 for successful input, 0 for end-of-file, and
    -1 for an error.

    The nawk command provides user-defined functions.  Such functions may be
    defined (in the pattern position of a pattern-action statement) as
    follows:

    function name(arg, ... ) { stmts }

    func name(arg, ... ) { stmts }

    Function arguments are passed by value if scalar and by reference if array
    name.  Argument names are local to the function; all other variable names
    are global.  Function calls may be nested and functions may be recursive.
    The return statement may be used to return a value.

  EXAMPLES

    1.  The following matches display lines longer than 72 characters:

        length > 72


    2.  The following displays the first two fields in the opposite order:

        { print $2, $1 }


    3.  The following displays the first two fields in the opposite order,
        with the input fields separated by a comma and/or blanks and tabs:

        BEGIN { FS = ",[\t]*|[\t]+" }



  2/94 - Intergraph Corporation                                              5






  nawk(1)                             CLIX                             nawk(1)



              { print $2, $1 }


    4.  The following adds up the first column, then displays the sum and
        average:

            { s += $1 }
        END { print "sum is", s, " average is", s/NR }


    5.  The following displays fields in reverse order:

        { for (i = NF; i > 0; --i) print $i }


    6.  The following displays all lines between start/stop pairs:

        /start/, /stop/


    7.  The following displays all lines whose first field is different from
        the previous one:

        $1 != prev { print; prev = $1 }


    8.  The following simulates an echo:

        BEGIN {
            for (i = 1; i < ARGC; i++)
            printf "%s", ARGV[i]
            printf "\n"
            exit
        }


    9.  The following displays file, filling in page numbers starting at 5:

        nawk n=5 file '/Page/ { $2 = n++; }
                       { print }'


  NOTES

    Input white space is not preserved on output if fields are involved.

    There are no explicit conversions between numbers and strings.  To force
    an expression to be treated as a number add 0 to it; to force it to be
    treated as a string concatenate the null string ("") to it.

  DIAGNOSTICS



  6                                              Intergraph Corporation - 2/94






  nawk(1)                             CLIX                             nawk(1)



    Not enough args in printf(list)
           A print string does not have enough variables to match the format
           string.

    Syntax error at line line
           A statement that does not conform to the awk syntax found at the
           line.

    name is not an array
           A subscript reference was made to an item that is not an array
           type.

  EXIT VALUES

    The nawk command exits with a value of 1 if an error occurs.

  RELATED INFORMATION

    Commands:  grep(1), sed(1), lex(1)

    Functions:  printf(3)

































  2/94 - Intergraph Corporation                                              7




Typewritten Software • bear@typewritten.org • Edmonds, WA 98026