home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / gawk213b / gawk.man < prev    next >
Encoding:
Text File  |  1993-07-29  |  37.6 KB  |  961 lines

  1.  
  2.  
  3.  
  4. GAWK(1)                 Utility Commands                  GAWK(1)
  5.  
  6.  
  7. NAME
  8.      gawk - pattern scanning and processing language
  9.  
  10. SYNOPSIS
  11.      gawk [ -W gawk-options ] [ -Ffs ] [ -v var=val ] -f
  12.      program-file [ -- ] file ...
  13.      gawk [ -W gawk-options ] [ -Ffs ] [ -v var=val ] [ -- ]
  14.      program-text file ...
  15.  
  16. DESCRIPTION
  17.      Gawk is the GNU Project's implementation of the AWK program-
  18.      ming language.  It conforms to the definition of the
  19.      language in the POSIX P1003.2 Command Language And Utilities
  20.      Standard (draft 11).  This version in turn is based on the
  21.      description in The AWK Programming Language, by Aho, Ker-
  22.      nighan, and Weinberger, with the additional features defined
  23.      in the System V Release 4 version of UNIX awk.  Gawk also
  24.      provides some GNU-specific extensions.
  25.  
  26.      The command line consists of options to gawk itself, the AWK
  27.      program text (if not supplied via the -f option), and values
  28.      to be made available in the ARGC and ARGV pre-defined AWK
  29.      variables.
  30.  
  31.      Gawk accepts the following options, which should be avail-
  32.      able on any implementation of the AWK language.
  33.  
  34.      -Ffs Use fs for the input field separator (the value of the
  35.           FS predefined variable).
  36.  
  37.      -v var=val
  38.           Assign the value val, to the variable var, before exe-
  39.           cution of the program begins.  Such variable values are
  40.           available to the BEGIN block of an AWK program.
  41.  
  42.      -f program-file
  43.           Read the AWK program source from the file program-file,
  44.           instead of from the first command line argument.  Mul-
  45.           tiple -f options may be used.
  46.  
  47.      --   Signal the end of options. This is useful to allow
  48.           further arguments to the AWK program itself to start
  49.           with a ``-''.  This is mainly for consistency with the
  50.           argument parsing convention used by most other POSIX
  51.           programs.
  52.  
  53.      Following the POSIX standard, gawk-specific options are sup-
  54.      plied via arguments to the -W option.  Multiple -W options
  55.      may be supplied, or multiple arguments may be supplied
  56.      together if they are separated by commas, or enclosed in
  57.      quotes and separated by white space.  Case is ignored in
  58.      arguments to the -W option.  Gawk recognizes the following
  59.      arguments to -W:
  60.  
  61.      compat    Run in compatibility mode.  In compatibility mode,
  62.                gawk behaves identically to UNIX awk; none of the
  63.                GNU-specific extensions are recognized.
  64.  
  65.      copyleft
  66.      copyright Print the short version of the GNU copyright
  67.                information message on the error output.
  68.  
  69.      version   Print version information for this particular copy
  70.                of gawk on the error output.  This is useful
  71.                mainly for knowing if the current copy of gawk on
  72.                your system is up to date with respect to whatever
  73.                the Free Software Foundation is distributing.
  74.  
  75.      posix     This turns on compatibility mode, with the follow-
  76.                ing additional restrictions:
  77.  
  78.                o    \x escape sequences are not recognized.
  79.  
  80.                o    The synonym func for the keyword function is
  81.                     not recognized.
  82.  
  83.                o    The operators ** and **= can not be used in
  84.                     place of ^ and ^=.
  85.  
  86.      lint      Provide warnings about constructs that are dubious
  87.                or non-portable to other AWK implementations.
  88.  
  89.      Any other options are flagged as illegal, but are otherwise
  90.      ignored.
  91.  
  92. AWK PROGRAM EXECUTION
  93.      An AWK program consists of a sequence of pattern-action
  94.      statements and optional function definitions.
  95.  
  96.           pattern   { action statements }
  97.           function name(parameter list) { statements }
  98.  
  99.      Gawk first reads the program source from the program-file(s)
  100.      if specified, or from the first non-option argument on the
  101.      command line.  The -f option may be used multiple times on
  102.      the command line.  Gawk will read the program text as if all
  103.      the program-files had been concatenated together.  This is
  104.      useful for building libraries of AWK functions, without hav-
  105.      ing to include them in each new AWK program that uses them.
  106.      To use a library function in a file from a program typed in
  107.      on the command line, specify /dev/tty as one of the
  108.      program-files, type your program, and end it with a ^D
  109.      (control-d).
  110.  
  111.      The environment variable AWKPATH specifies a search path to
  112.      use when finding source files named with the -f option.  If
  113.      this variable does not exist, the default path is
  114.      ".:/usr/lib/awk:/usr/local/lib/awk".  If a file name given
  115.      to the -f option contains a ``/'' character, no path search
  116.      is performed.
  117.  
  118.      Gawk executes AWK programs in the following order.  First,
  119.      gawk compiles the program into an internal form.  Next, all
  120.      variable assignments specified via the -v option are per-
  121.      formed.  Then, gawk executes the code in the BEGIN block(s)
  122.      (if any), and then proceeds to read each file named in the
  123.      ARGV array.  If there are no files named on the command
  124.      line, gawk reads the standard input.
  125.  
  126.      If a ``file'' named on the command line has the form var=val
  127.      it is treated as a variable assignment. The variable var
  128.      will be assigned the value val.  This is most useful for
  129.      dynamically assigning values to the variables AWK uses to
  130.      control how input is broken into fields and records. It is
  131.      also useful for controlling state if multiple passes are
  132.      needed over a single data file.
  133.  
  134.      If the value of a particular element of ARGV is empty (""),
  135.      gawk skips over it.
  136.  
  137.      For each line in the input, gawk tests to see if it matches
  138.      any pattern in the AWK program.  For each pattern that the
  139.      line matches, the associated action is executed.  The pat-
  140.      terns are tested in the order they occur in the program.
  141.  
  142.      Finally, after all the input is exhausted, gawk executes the
  143.      code in the END block(s) (if any).
  144.  
  145. VARIABLES AND FIELDS
  146.      AWK variables are dynamic; they come into existence when
  147.      they are first used. Their values are either floating-point
  148.      numbers or strings, or both, depending upon how they are
  149.      used. AWK also has one dimension arrays; multiply dimen-
  150.      sioned arrays may be simulated.  There are several pre-
  151.      defined variables that AWK sets as a program runs; these
  152.      will be described as needed and summarized below.
  153.  
  154.      Fields
  155.  
  156.      As each input line is read, gawk splits the line into
  157.      fields, using the value of the FS variable as the field
  158.      separator.  If FS is a single character, fields are
  159.      separated by that character.  Otherwise, FS is expected to
  160.      be a full regular expression.  In the special case that FS
  161.      is a single blank, fields are separated by runs of blanks
  162.      and/or tabs.  Note that the value of IGNORECASE (see below)
  163.      will also affect how fields are split when FS is a regular
  164.      expression.
  165.  
  166.      If the FIELDWIDTHS variable is set to a space separated list
  167.      of numbers, each field is expected to have fixed width, and
  168.      gawk will split up the record using the specified widths.
  169.      The value of FS is ignored.
  170.  
  171.      Each field in the input line may be referenced by its posi-
  172.      tion, $1, $2, and so on.  $0 is the whole line. The value of
  173.      a field may be assigned to as well.  Fields need not be
  174.      referenced by constants:
  175.  
  176.           n = 5
  177.           print $n
  178.  
  179.      prints the fifth field in the input line.  The variable NF
  180.      is set to the total number of fields in the input line.
  181.  
  182.      References to non-existent fields (i.e. fields after $NF),
  183.      produce the null-string. However, assigning to a non-
  184.      existent field (e.g., $(NF+2) = 5) will increase the value
  185.      of NF, create any intervening fields with the null string as
  186.      their value, and cause the value of $0 to be recomputed,
  187.      with the fields being separated by the value of OFS.
  188.  
  189.      Built-in Variables
  190.  
  191.      AWK's built-in variables are:
  192.  
  193.      ARGC        The number of command line arguments (does not
  194.                  include options to gawk, or the program source).
  195.  
  196.      ARGV        Array of command line arguments. The array is
  197.                  indexed from 0 to ARGC - 1.  Dynamically
  198.                  changing the contents of ARGV can control the
  199.                  files used for data.
  200.  
  201.      CONVFMT     The conversion format for numbers, "%.6g", by
  202.                  default.
  203.  
  204.      ENVIRON     An array containing the values of the current
  205.                  environment.  The array is indexed by the
  206.                  environment variables, each element being the
  207.                  value of that variable (e.g., ENVIRON["HOME"]
  208.                  might be /u/arnold).  Changing this array does
  209.                  not affect the environment seen by programs
  210.                  which gawk spawns via redirection or the sys-
  211.                  tem() function.  (This may change in a future
  212.                  version of gawk.)
  213.  
  214.      FIELDWIDTHS A white-space separated list of fieldwidths.
  215.                  When set, gawk parses the input into fields of
  216.                  fixed width, instead of using the value of the
  217.                  FS variable.
  218.  
  219.      FILENAME    The name of the current input file.  If no files
  220.                  are specified on the command line, the value of
  221.                  FILENAME is ``-''.
  222.  
  223.      FNR         The input record number in the current input
  224.                  file.
  225.  
  226.      FS          The input field separator, a blank by default.
  227.  
  228.      IGNORECASE  Controls the case-sensitivity of all regular
  229.                  expression operations. If IGNORECASE has a non-
  230.                  zero value, then pattern matching in rules,
  231.                  field splitting with FS, regular expression
  232.                  matching with ~ and !~, and the gsub(), index(),
  233.                  match(), split(), and sub() pre-defined func-
  234.                  tions will all ignore case when doing regular
  235.                  expression operations.  Thus, if IGNORECASE is
  236.                  not equal to zero, /aB/ matches all of the
  237.                  strings "ab", "aB", "Ab", and "AB".  As with all
  238.                  AWK variables, the initial value of IGNORECASE
  239.                  is zero, so all regular expression operations
  240.                  are normally case-sensitive.
  241.  
  242.      NF          The number of fields in the current input
  243.                  record.
  244.  
  245.      NR          The total number of input records seen so far.
  246.  
  247.      OFMT        The output format for numbers, "%.6g", by
  248.                  default.
  249.  
  250.      OFS         The output field separator, a blank by default.
  251.  
  252.      ORS         The output record separator, by default a new-
  253.                  line.
  254.  
  255.      RS          The input record separator, by default a new-
  256.                  line.  RS is exceptional in that only the first
  257.                  character of its string value is used for
  258.                  separating records. If RS is set to the null
  259.                  string, then records are separated by blank
  260.                  lines.  When RS is set to the null string, then
  261.                  the newline character always acts as a field
  262.                  separator, in addition to whatever value FS may
  263.                  have.
  264.  
  265.      RSTART      The index of the first character matched by
  266.                  match(); 0 if no match.
  267.  
  268.      RLENGTH     The length of the string matched by match(); -1
  269.                  if no match.
  270.  
  271.      SUBSEP      The character used to separate multiple sub-
  272.                  scripts in array elements, by default "\034".
  273.  
  274.      Arrays
  275.  
  276.      Arrays are subscripted with an expression between square
  277.      brackets ([ and ]).  If the expression is an expression list
  278.      (expr, expr ...) then the array subscript is a string con-
  279.      sisting of the concatenation of the (string) value of each
  280.      expression, separated by the value of the SUBSEP variable.
  281.      This facility is used to simulate multiply dimensioned
  282.      arrays. For example:
  283.  
  284.           i = "A" ; j = "B" ; k = "C"
  285.           x[i, j, k] = "hello, world\n"
  286.  
  287.      assigns the string "hello, world\n" to the element of the
  288.      array x which is indexed by the string "A\034B\034C". All
  289.      arrays in AWK are associative, i.e. indexed by string
  290.      values.
  291.  
  292.      The special operator in may be used in an if or while state-
  293.      ment to see if an array has an index consisting of a partic-
  294.      ular value.
  295.  
  296.           if (val in array)
  297.                print array[val]
  298.  
  299.      If the array has multiple subscripts, use (i, j) in array.
  300.  
  301.      The in construct may also be used in a for loop to iterate
  302.      over all the elements of an array.
  303.  
  304.      An element may be deleted from an array using the delete
  305.      statement.
  306.  
  307.      Variable Typing
  308.  
  309.      Variables and fields may be (floating point) numbers, or
  310.      strings, or both. How the value of a variable is interpreted
  311.      depends upon its context. If used in a numeric expression,
  312.      it will be treated as a number, if used as a string it will
  313.      be treated as a string.
  314.  
  315.      To force a variable to be treated as a number, add 0 to it;
  316.      to force it to be treated as a string, concatenate it with
  317.      the null string.
  318.  
  319.      Gawk performs comparisons as follows: If two variables are
  320.      numeric, they are compared numerically.  If one value is
  321.      numeric and the other has a string value that is a ``numeric
  322.      string,'' then comparisons are also done numerically.
  323.      Otherwise, the numeric value is converted to a string and a
  324.      string comparison is performed.  Two strings are compared,
  325.      of course, as strings.  According to the POSIX standard
  326.      (draft 11), even if two strings are numeric strings, a
  327.      numeric comparison is performed.  However, this is clearly
  328.      incorrect, and gawk does not do this.
  329.  
  330.      Uninitialized variables have the numeric value 0 and the
  331.      string value "" (the null, or empty, string).
  332.  
  333. PATTERNS AND ACTIONS
  334.      AWK is a line oriented language. The pattern comes first,
  335.      and then the action. Action statements are enclosed in { and
  336.      }.  Either the pattern may be missing, or the action may be
  337.      missing, but, of course, not both. If the pattern is miss-
  338.      ing, the action will be executed for every single line of
  339.      input.  A missing action is equivalent to
  340.  
  341.           { print }
  342.  
  343.      which prints the entire line.
  344.  
  345.      Comments begin with the ``#'' character, and continue until
  346.      the end of the line.  Blank lines may be used to separate
  347.      statements.  Normally, a statement ends with a newline, how-
  348.      ever, this is not the case for lines ending in a ``,'',
  349.      ``{'', ``?'', ``:'', ``&&'', or ``||''.  Lines ending in do
  350.      or else also have their statements automatically continued
  351.      on the following line.  In other cases, a line can be con-
  352.      tinued by ending it with a ``\'', in which case the newline
  353.      will be ignored.
  354.  
  355.      Multiple statements may be put on one line by separating
  356.      them with a ``;''.  This applies to both the statements
  357.      within the action part of a pattern-action pair (the usual
  358.      case), and to the pattern-action statements themselves.
  359.  
  360.      Patterns
  361.      AWK patterns may be one of the following:
  362.  
  363.           BEGIN
  364.           END
  365.           /regular expression/
  366.           relational expression
  367.           pattern && pattern
  368.           pattern || pattern
  369.           pattern ? pattern : pattern
  370.           (pattern)
  371.           ! pattern
  372.           pattern1, pattern2
  373.  
  374.      BEGIN and END are two special kinds of patterns which are
  375.      not tested against the input.  The action parts of all BEGIN
  376.      patterns are merged as if all the statements had been writ-
  377.      ten in a single BEGIN block. They are executed before any of
  378.      the input is read. Similarly, all the END blocks are merged,
  379.      and executed when all the input is exhausted (or when an
  380.      exit statement is executed).  BEGIN and END patterns cannot
  381.      be combined with other patterns in pattern expressions.
  382.      BEGIN and END patterns cannot have missing action parts.
  383.  
  384.      For /regular expression/ patterns, the associated statement
  385.      is executed for each input line that matches the regular
  386.      expression.  Regular expressions are the same as those in
  387.      egrep(1), and are summarized below.
  388.  
  389.      A relational expression may use any of the operators defined
  390.      below in the section on actions.  These generally test
  391.      whether certain fields match certain regular expressions.
  392.  
  393.      The &&, ||, and ! operators are logical AND, logical OR, and
  394.      logical NOT, respectively, as in C.  They do short-circuit
  395.      evaluation, also as in C, and are used for combining more
  396.      primitive pattern expressions. As in most languages,
  397.      parentheses may be used to change the order of evaluation.
  398.  
  399.      The ?: operator is like the same operator in C. If the first
  400.      pattern is true then the pattern used for testing is the
  401.      second pattern, otherwise it is the third. Only one of the
  402.      second and third patterns is evaluated.
  403.  
  404.      The pattern1, pattern2 form of an expression is called a
  405.      range pattern.  It matches all input records starting with a
  406.      line that matches pattern1, and continuing until a record
  407.      that matches pattern2, inclusive. It does not combine with
  408.      any other sort of pattern expression.
  409.  
  410.      Regular Expressions
  411.      Regular expressions are the extended kind found in egrep.
  412.      They are composed of characters as follows:
  413.  
  414.      c         matches the non-metacharacter c.
  415.  
  416.      \c        matches the literal character c.
  417.  
  418.      .         matches any character except newline.
  419.  
  420.      ^         matches the beginning of a line or a string.
  421.  
  422.      $         matches the end of a line or a string.
  423.  
  424.      [abc...]  character class, matches any of the characters
  425.                abc....
  426.  
  427.      [^abc...] negated character class, matches any character
  428.                except abc... and newline.
  429.  
  430.      r1|r2     alternation: matches either r1 or r2.
  431.  
  432.      r1r2      concatenation: matches r1, and then r2.
  433.  
  434.      r+        matches one or more r's.
  435.  
  436.      r*        matches zero or more r's.
  437.  
  438.      r?        matches zero or one r's.
  439.  
  440.      (r)       grouping: matches r.
  441.  
  442.      The escape sequences that are valid in string constants (see
  443.      below) are also legal in regular expressions.
  444.  
  445.      Actions
  446.      Action statements are enclosed in braces, { and }.  Action
  447.      statements consist of the usual assignment, conditional, and
  448.      looping statements found in most languages. The operators,
  449.      control statements, and input/output statements available
  450.      are patterned after those in C.
  451.  
  452.      Operators
  453.  
  454.      The operators in AWK, in order of increasing precedence, are
  455.  
  456.      = += -= *= /= %= ^= Assignment. Both absolute assignment
  457.                          (var = value) and operator-assignment
  458.                          (the other forms) are supported.
  459.  
  460.      ?:                  The C conditional expression. This has
  461.                          the form expr1 ? expr2 : expr3. If expr1
  462.                          is true, the value of the expression is
  463.                          expr2, otherwise it is expr3.  Only one
  464.                          of expr2 and expr3 is evaluated.
  465.  
  466.      ||                  Logical OR.
  467.  
  468.      &&                  Logical AND.
  469.  
  470.      ~ !~                Regular expression match, negated match.
  471.                          NOTE: Do not use a constant regular
  472.                          expression (/foo/) on the left-hand side
  473.                          of a ~ or !~.  Only use one on the
  474.                          right-hand side.  The expression /foo/ ~
  475.                          exp has the same meaning as (($0 ~
  476.                          /foo/) ~ exp).  This is usually not what
  477.                          was intended.
  478.  
  479.      < <= > >= != ==     The regular relational operators.
  480.  
  481.      blank               String concatenation.
  482.  
  483.      + -                 Addition and subtraction.
  484.  
  485.      * / %               Multiplication, division, and modulus.
  486.  
  487.      + - !               Unary plus, unary minus, and logical
  488.                          negation.
  489.  
  490.      ^                   Exponentiation (** may also be used, and
  491.                          **= for the assignment operator).
  492.  
  493.      ++ --               Increment and decrement, both prefix and
  494.                          postfix.
  495.  
  496.      $                   Field reference.
  497.  
  498.      Control Statements
  499.  
  500.      The control statements are as follows:
  501.  
  502.           if (condition) statement [ else statement ]
  503.           while (condition) statement
  504.           do statement while (condition)
  505.           for (expr1; expr2; expr3) statement
  506.           for (var in array) statement
  507.           break
  508.           continue
  509.           delete array[index]
  510.           exit [ expression ]
  511.           { statements }
  512.  
  513.      I/O Statements
  514.  
  515.      The input/output statements are as follows:
  516.  
  517.      close(filename)       Close file (or pipe, see below).
  518.  
  519.      getline               Set $0 from next input record; set NF,
  520.                            NR, FNR.
  521.  
  522.      getline <file         Set $0 from next record of file; set
  523.                            NF.
  524.  
  525.      getline var           Set var from next input record; set
  526.                            NF, FNR.
  527.  
  528.      getline var <file     Set var from next record of file.
  529.  
  530.      next                  Stop processing the current input
  531.                            record. The next input record is read
  532.                            and processing starts over with the
  533.                            first pattern in the AWK program. If
  534.                            the end of the input data is reached,
  535.                            the END block(s), if any, are exe-
  536.                            cuted.
  537.  
  538.      print                 Prints the current record.
  539.  
  540.      print expr-list       Prints expressions.
  541.  
  542.      print expr-list >file Prints expressions on file.
  543.  
  544.      printf fmt, expr-list Format and print.
  545.  
  546.      printf fmt, expr-list >file
  547.                            Format and print on file.
  548.  
  549.      system(cmd-line)      Execute the command cmd-line, and
  550.                            return the exit status.  (This may not
  551.                            be available on non-POSIX systems.)
  552.  
  553.      Other input/output redirections are also allowed. For print
  554.      and printf, >>file appends output to the file, while | com-
  555.      mand writes on a pipe.  In a similar fashion, command | get-
  556.      line pipes into getline.  Getline will return 0 on end of
  557.      file, and -1 on an error.
  558.  
  559.      The printf Statement
  560.  
  561.      The AWK versions of the printf and sprintf (see below) func-
  562.      tions accept the following conversion specification formats:
  563.  
  564.      %c   An ASCII character.  If the argument used for %c is
  565.           numeric, it is treated as a character and printed.
  566.           Otherwise, the argument is assumed to be a string, and
  567.           the only first character of that string is printed.
  568.  
  569.      %d   A decimal number (the integer part).
  570.  
  571.      %i   Just like %d.
  572.  
  573.      %e   A floating point number of the form [-]d.ddddddE[+-]dd.
  574.  
  575.      %f   A floating point number of the form [-]ddd.dddddd.
  576.  
  577.      %g   Use e or f conversion, whichever is shorter, with non-
  578.           significant zeros suppressed.
  579.  
  580.      %o   An unsigned octal number (again, an integer).
  581.  
  582.      %s   A character string.
  583.  
  584.      %x   An unsigned hexadecimal number (an integer).
  585.  
  586.      %X   Like %x, but using ABCDEF instead of abcdef.
  587.  
  588.      %%   A single % character; no argument is converted.
  589.  
  590.      There are optional, additional parameters that may lie
  591.      between the % and the control letter:
  592.  
  593.      -    The expression should be left-justified within its
  594.           field.
  595.  
  596.      width
  597.           The field should be padded to this width. If the number
  598.           has a leading zero, then the field will be padded with
  599.           zeros.  Otherwise it is padded with blanks.
  600.  
  601.      .prec
  602.           A number indicating the maximum width of strings or
  603.           digits to the right of the decimal point.
  604.  
  605.      The dynamic width and prec capabilities of the ANSI C printf
  606.      routines are supported.  A * in place of either the width or
  607.      prec specifications will cause their values to be taken from
  608.      the argument list to printf or sprintf.
  609.  
  610.      Special File Names
  611.  
  612.      When doing I/O redirection from either print or printf into
  613.      a file, or via getline from a file, gawk recognizes certain
  614.      special filenames internally.  These filenames allow access
  615.      to open file descriptors inherited from gawk's parent pro-
  616.      cess (usually the shell).  The filenames are:
  617.  
  618.      /dev/stdin
  619.           The standard input.
  620.  
  621.      /dev/stdout
  622.           The standard output.
  623.  
  624.      /dev/stderr
  625.           The standard error output.
  626.  
  627.      /dev/fd/n
  628.           The file denoted by the open file descriptor n.
  629.  
  630.      These are particularly useful for error messages. For exam-
  631.      ple:
  632.  
  633.           print "You blew it!" > "/dev/stderr"
  634.  
  635.      whereas you would otherwise have to use
  636.  
  637.           print "You blew it!" | "cat 1>&2"
  638.  
  639.      These file names may also be used on the command line to
  640.      name data files.
  641.  
  642.      Numeric Functions
  643.  
  644.      AWK has the following pre-defined arithmetic functions:
  645.  
  646.      atan2(y, x) returns the arctangent of y/x in radians.
  647.  
  648.      cos(expr)   returns the cosine in radians.
  649.  
  650.      exp(expr)   the exponential function.
  651.  
  652.      int(expr)   truncates to integer.
  653.  
  654.      log(expr)   the natural logarithm function.
  655.  
  656.      rand()      returns a random number between 0 and 1.
  657.  
  658.      sin(expr)   returns the sine in radians.
  659.  
  660.      sqrt(expr)  the square root function.
  661.  
  662.      srand(expr) use expr as a new seed for the random number
  663.                  generator. If no expr is provided, the time of
  664.                  day will be used.  The return value is the pre-
  665.                  vious seed for the random number generator.
  666.  
  667.      String Functions
  668.  
  669.      AWK has the following pre-defined string functions:
  670.  
  671.      gsub(r, s, t)           for each substring matching the reg-
  672.                              ular expression r in the string t,
  673.                              substitute the string s, and return
  674.                              the number of substitutions.  If t
  675.                              is not supplied, use $0.
  676.  
  677.      index(s, t)             returns the index of the string t in
  678.                              the string s, or 0 if t is not
  679.                              present.
  680.  
  681.      length(s)               returns the length of the string s,
  682.                              or the length of $0 if s is not sup-
  683.                              plied.
  684.  
  685.      match(s, r)             returns the position in s where the
  686.                              regular expression r occurs, or 0 if
  687.                              r is not present, and sets the
  688.                              values of RSTART and RLENGTH.
  689.  
  690.      split(s, a, r)          splits the string s into the array a
  691.                              on the regular expression r, and
  692.                              returns the number of fields. If r
  693.                              is omitted, FS is used instead.
  694.  
  695.      sprintf(fmt, expr-list) prints expr-list according to fmt,
  696.                              and returns the resulting string.
  697.  
  698.      sub(r, s, t)            just like gsub, but only the first
  699.                              matching substring is replaced.
  700.  
  701.      substr(s, i, n)         returns the n-character substring of
  702.                              s starting at i.  If n is omitted,
  703.                              the rest of s is used.
  704.  
  705.      tolower(str)            returns a copy of the string str,
  706.                              with all the upper-case characters
  707.                              in str translated to their
  708.                              corresponding lower-case counter-
  709.                              parts.  Non-alphabetic characters
  710.                              are left unchanged.
  711.  
  712.      toupper(str)            returns a copy of the string str,
  713.                              with all the lower-case characters
  714.                              in str translated to their
  715.                              corresponding upper-case counter-
  716.                              parts.  Non-alphabetic characters
  717.                              are left unchanged.
  718.  
  719.      Time Functions
  720.  
  721.      Since one of the primary uses of AWK programs in processing
  722.      log files that contain time stamp information, gawk provides
  723.      the following two functions for obtaining time stamps and
  724.      formatting them.
  725.  
  726.      systime()                   returns the current time of day
  727.                                  as the number of seconds since
  728.                                  the Epoch (Midnight UTC, January
  729.                                  1, 1970 on POSIX systems).
  730.  
  731.      strftime(format, timestamp) formats timestamp according to
  732.                                  the specification in format.
  733.                                  The timestamp should be of the
  734.                                  same form as returned by sys-
  735.                                  time().  If timestamp is miss-
  736.                                  ing, the current time of day is
  737.                                  used.  See the specification for
  738.                                  the strftime function in ANSI C
  739.                                  for the format conversions that
  740.                                  are guaranteed to be available.
  741.                                  A public-domain version of
  742.                                  strftime(3) and a man page for
  743.                                  it are shipped with gawk; if
  744.                                  that version was used to build
  745.                                  gawk, then all of the conver-
  746.                                  sions described in that man page
  747.                                  are available to gawk.
  748.  
  749.      String Constants
  750.  
  751.      String constants in AWK are sequences of characters enclosed
  752.      between double quotes ("). Within strings, certain escape
  753.      sequences are recognized, as in C. These are:
  754.  
  755.      \\   A literal backslash.
  756.  
  757.      \a   The ``alert'' character; usually the ASCII BEL charac-
  758.           ter.
  759.  
  760.      \b   backspace.
  761.  
  762.      \f   form-feed.
  763.  
  764.      \n   new line.
  765.  
  766.      \r   carriage return.
  767.  
  768.      \t   horizontal tab.
  769.  
  770.      \v   vertical tab.
  771.  
  772.      \xhex digits
  773.           The character represented by the string of hexadecimal
  774.           digits following the \x.  As in ANSI C, all following
  775.           hexadecimal digits are considered part of the escape
  776.           sequence.  (This feature should tell us something about
  777.           language design by committee.) E.g., "\x1B" is the
  778.           ASCII ESC (escape) character.
  779.  
  780.      \ddd The character represented by the 1-, 2-, or 3-digit
  781.           sequence of octal digits. E.g. "\033" is the ASCII ESC
  782.           (escape) character.
  783.  
  784.      \c   The literal character c.
  785.  
  786.      The escape sequences may also be used inside constant regu-
  787.      lar expressions (e.g., /[ \t\f\n\r\v]/ matches whitespace
  788.      characters).
  789.  
  790. FUNCTIONS
  791.      Functions in AWK are defined as follows:
  792.  
  793.           function name(parameter list) { statements }
  794.  
  795.      Functions are executed when called from within the action
  796.      parts of regular pattern-action statements. Actual parame-
  797.      ters supplied in the function call are used to instantiate
  798.      the formal parameters declared in the function.  Arrays are
  799.      passed by reference, other variables are passed by value.
  800.  
  801.      Since functions were not originally part of the AWK
  802.      language, the provision for local variables is rather
  803.      clumsy: they are declared as extra parameters in the parame-
  804.      ter list. The convention is to separate local variables from
  805.      real parameters by extra spaces in the parameter list. For
  806.      example:
  807.  
  808.           function  f(p, q,     a, b) { # a & b are local
  809.                          ..... }
  810.  
  811.           /abc/     { ... ; f(1, 2) ; ... }
  812.  
  813.      The left parenthesis in a function call is required to
  814.      immediately follow the function name, without any interven-
  815.      ing white space.  This is to avoid a syntactic ambiguity
  816.      with the concatenation operator.  This restriction does not
  817.      apply to the built-in functions listed above.
  818.  
  819.      Functions may call each other and may be recursive.  Func-
  820.      tion parameters used as local variables are initialized to
  821.      the null string and the number zero upon function invoca-
  822.      tion.
  823.  
  824.      The word func may be used in place of function.
  825.  
  826. EXAMPLES
  827.      Print and sort the login names of all users:
  828.  
  829.           BEGIN     { FS = ":" }
  830.                { print $1 | "sort" }
  831.  
  832.      Count lines in a file:
  833.  
  834.                { nlines++ }
  835.           END  { print nlines }
  836.  
  837.      Precede each line by its number in the file:
  838.  
  839.           { print FNR, $0 }
  840.  
  841.      Concatenate and line number (a variation on a theme):
  842.  
  843.           { print NR, $0 }
  844.  
  845. SEE ALSO
  846.      egrep(1)
  847.  
  848.      The AWK Programming Language, Alfred V. Aho, Brian W. Ker-
  849.      nighan, Peter J. Weinberger, Addison-Wesley, 1988. ISBN 0-
  850.      201-07981-X.
  851.  
  852.      The GAWK Manual, published by the Free Software Foundation,
  853.      1991.
  854.  
  855. POSIX COMPATIBILITY
  856.      A primary goal for gawk is compatibility with the POSIX
  857.      standard, as well as with the latest version of UNIX awk.
  858.      To this end, gawk incorporates the following user visible
  859.      features which are not described in the AWK book, but are
  860.      part of awk in System V Release 4, and are in the POSIX
  861.      standard.
  862.  
  863.      The -v option for assigning variables before program execu-
  864.      tion starts is new.  The book indicates that command line
  865.      variable assignment happens when awk would otherwise open
  866.      the argument as a file, which is after the BEGIN block is
  867.      executed.  However, in earlier implementations, when such an
  868.      assignment appeared before any file names, the assignment
  869.      would happen before the BEGIN block was run.  Applications
  870.      came to depend on this ``feature.'' When awk was changed to
  871.      match its documentation, this option was added to accomodate
  872.      applications that depended upon the old behaviour.  (This
  873.      feature was agreed upon by both the AT&T and GNU develop-
  874.      ers.)
  875.  
  876.      The -W option for implementation specific features is from
  877.      the POSIX standard.
  878.  
  879.      When processing arguments, gawk uses the special option
  880.      ``--'' to signal the end of arguments, and warns about, but
  881.      otherwise ignores, undefined options.
  882.  
  883.      The AWK book does not define the return value of srand().
  884.      The System V Release 4 version of UNIX awk (and the POSIX
  885.      standard) has it return the seed it was using, to allow
  886.      keeping track of random number sequences. Therefore srand()
  887.      in gawk also returns its current seed.
  888.  
  889.      Other new features are: The use of multiple -f options (from
  890.      MKS awk); the ENVIRON array; the \a, and \v escape sequences
  891.      (done originally in gawk and fed back into AT&T's); the
  892.      tolower and toupper built-in functions (from AT&T); and the
  893.      ANSI C conversion specifications in printf (done first in
  894.      AT&T's version).
  895.  
  896. GNU EXTENSIONS
  897.      Gawk has some extensions to POSIX awk.  They are described
  898.      in this section.  All the extensions described here can be
  899.      disabled by invoking gawk with the -W compat option.
  900.  
  901.      The following features of gawk are not available in POSIX
  902.      awk.
  903.  
  904.           o The \x escape sequence.
  905.  
  906.           o The systime() and strftime(format, secs) functions.
  907.  
  908.           o The special file names available for I/O redirection
  909.             are not recognized.
  910.  
  911.           o The IGNORECASE variable and its side-effects are not
  912.             available.
  913.  
  914.           o The FIELDWIDTHS variable and fixed width field split-
  915.             ting.
  916.  
  917.           o No path search is performed for files named via the
  918.             -f option.  Therefore the AWKPATH environment vari-
  919.             able is not special.
  920.  
  921.      The AWK book does not define the return value of the close()
  922.      function.  Gawk's close() returns the value from fclose(3),
  923.      or pclose(3), when closing a file or pipe, respectively.
  924.  
  925.      When gawk is invoked with the -W compat option, if the fs
  926.      argument to the -F option is ``t'', then FS will be set to
  927.      the tab character.  Since this is a rather ugly special
  928.      case, it is not the default behavior.
  929.  
  930. BUGS
  931.      The -F option is not necessary given the command line vari-
  932.      able assignment feature; it remains only for backwards com-
  933.      patibility.
  934.  
  935. VERSION INFORMATION
  936.      This man page documents gawk, version 2.13.
  937.  
  938.      For the 2.13 version of gawk, the -c, -v, -V, -a, and -e
  939.      options of the 2.11 version are recognized.  However, gawk
  940.      will print a warning message, and these options will go away
  941.      in the 2.14 version.
  942.  
  943. AUTHORS
  944.      The original version of UNIX awk was designed and imple-
  945.      mented by Alfred Aho, Peter Weinberger, and Brian Kernighan
  946.      of AT&T Bell Labs. Brian Kernighan continues to maintain and
  947.      enhance it.
  948.  
  949.      Paul Rubin and Jay Fenlason, of the Free Software Founda-
  950.      tion, wrote gawk, to be compatible with the original version
  951.      of awk distributed in Seventh Edition UNIX.  John Woods con-
  952.      tributed a number of bug fixes.  David Trueman of Dalhousie
  953.      University, with contributions from Arnold Robbins at Emory
  954.      University and AudioFAX, made gawk compatible with the new
  955.      version of UNIX awk.
  956.  
  957. ACKNOWLEDGEMENTS
  958.      Brian Kernighan of Bell Labs provided valuable assistance
  959.      during testing and debugging.  We thank him.
  960.  
  961.