Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ awk(C) — Xenix 2.3.4

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

grep(C)

lex(CP)

malloc(S)

sed(C)



     AWK(C)                   XENIX System V                    AWK(C)



     Name
          awk - Searches for and processes a pattern in a file.

     Syntax
          awk [ -Fc ] [ -f programfile | 'program' ] [ parameters ] [
          files ]

     Description
          awk scans each input file for lines that match patterns
          specified in program or in programfile.  When a line of
          files matches a pattern, an associated action may be
          performed. awk is useful for compiling information,
          performing arithmetic on input data, and for doing iterative
          or conditional processing.

          The options are:

          -Fc         Sets the field separator variable (FS) to the
                      letter ``c''. The default field separators are
                      tab and space.

          -f          Causes awk to take its program from programfile.

          The arguments are:

          programfile A file containing an awk program.

          program     An awk program. Programs given on the command
                      line must be enclosed in single quotation marks
                      to prevent interpretation by the shell.

          parameters  May be passed to awk in the form x=..., y=...,
                      etc.

          files       The name(s) of the file or files to be
                      processed. If no filename is given, the standard
                      input is used.

          An awk program consists of statements in the form:

               pattern { action }

          Pattern-action statements may appear on the awk command line
          or in an awk program file.

          If no pattern is given, all lines in the input file are
          matched. If no action is given, each matched line is
          displayed on the standard output.

          A pattern may be a literal string or a regular expression,
          or a combination of a regular expression and a field or
          variable separated by operators.



     Page 1                                           (printed 8/7/87)





     AWK(C)                   XENIX System V                    AWK(C)



          awk also provides two patterns, BEGIN and END, that can be
          used to perform actions before the first line is read and
          after the last line is read, respectively.

          To select a range of lines, use two patterns on a single
          program line, separated by a comma.

          An action is a sequence of statements separated by a
          semicolon, newline, or right brace. See Statements later in
          this section.

        Variables
          In addition to variables declared and initialized by the
          user, awk has the following program variables:

          NR        Number of records.

          NF        Number of fields in a record.

          FS        Input field separator.

          OFS       Output field separator.

          RS        Input record separator.

          ORS       Output record separator.

          $0        The current record.

          $1, $n    Fields in the current record.

          OFM       The output format for numbers. The default is
                    %.6g.

          FILENAME  The name of the input file currently being read.

          Arrays may be used to store data. Arrays do not need to be
          dimensioned before use. For example, ``w[i]'' denotes the
          ith item of array w.

        Expressions
          A pattern match with a field or variable may be tested with
          the following operators:

          ~    Matches the regular expression.

          !~   Does not match the regular expression.

          awk processes relational expressions using the following
          operators:

          <        Less than



     Page 2                                           (printed 8/7/87)





     AWK(C)                   XENIX System V                    AWK(C)



          <=       Less than or equal to

          ==       Equal to

          !=       Not equal to

          >=       Greater than or equal to

          >        Greater than

          Patterns can be combined using the operators:

          &&       And

          ||       Or

          !        Not

          An empty expression-list stands for the whole line.
          Expressions take on string or numeric values as appropriate,
          and are built using the following operators:

          +        Addition

          -        Subtraction

          *        Multiplication

          /        Division

          %        Modulo

          Concatenation is indicated by a blank.

          The following C operators are also available in expressions:

          ++       Increment

          - -      Decrement

          +=       Add and assign

          -=       Subtract and assign

          *=       Multiply and assign

          /=       Divide and assign

          %=       Modulo and assign

        Statements
               if ( conditional ) statement [ else statement ]



     Page 3                                           (printed 8/7/87)





     AWK(C)                   XENIX System V                    AWK(C)



               while ( conditional ) statement
               for ( expression ; conditional ; expression ) statement
               break
               continue
               { [ statement ] ... }
               variable = expression
               print [ expression-list ] [ >expression ]
               printf format [ , expression-list ] [ >expression ]
               next      #skip remaining patterns on input line

          while    Used the same as in C.

          for      The iterative construction. It can be used the same
                   as in the C language, or as an array iterator.

          break    Similar to its C counterpart.

          continue Similar to its C counterpart.

          print    Prints its arguments on the standard output, or in
                   a file if redirected.

          printf   Prints expression-list in the format specified in
                   format. See printf(S).

          next     Stops processing the current record and moves to
                   the next record, if any.

          Comments are preceded by a number sign (#).

        Functions
          awk has the following built-in functions:

          exit(x)     Terminates the awk program. If x is given, this
                      value is awk's return value. If x is not given,
                      0 is returned.  If the program has an END
                      section, it is invoked before termination.

          exp(x)      Exponentiation of the value of x.

          index(s, t) Returns the starting position of the leftmost
                      occurrence of t in s. If t is not a substring of
                      s, then index(s, t) is 0.

          int(x)      Returns the largest integer less than or equal
                      to x. If x is negative, its value is the
                      smallest integer greater than or equal to x.

          length(x)   A function whose value is the number of
                      characters in the string (x). With no arguments,
                      length is equivalent to $0.




     Page 4                                           (printed 8/7/87)





     AWK(C)                   XENIX System V                    AWK(C)



          log(x)      Natural logarithm of x.

          split(x, y) Assigns the fields of string x to successive
                      elements of array y.

          sqrt(x)     Square root of x.

          substr(string, index, length)
                      Returns the substring of string that begins at
                      index and is length characters long.

     Examples
          The following displays lines in file longer than 72
          characters:

               awk '{length > 72}' file

          The following prints the first two fields in opposite order:

               awk '{ print $2, $1 }' file

          The following adds up the first columns and prints their sum
          and average:

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

          The following prints the  fields in file in reverse order:

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

          The following prints all lines between start/stop pairs:

               awk '{/start/, /stop/}' file





















     Page 5                                           (printed 8/7/87)





     AWK(C)                   XENIX System V                    AWK(C)



          The following awk program file will print all lines in the
          object file whose first field is different from the first
          field in the previous line:

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

          The following program prints a file, filling in page numbers
          starting at 5:

               /Page/ {$2 = n++;}
                      {print}

               The command line has the form: awk -f program n=5 input

     See Also
          grep(C), lex(CP), malloc(S), sed(C)
          XENIX Text Processing Guide

     Notes
          Input whitespace 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.

          This command is explained in detail in the XENIX Text
          Processing Guide.


























     Page 6                                           (printed 8/7/87)



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