home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gawk / gawk215b.zoo / gawk.man < prev    next >
Encoding:
Text File  |  1993-06-28  |  42.0 KB  |  1,093 lines

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