home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / vms / gawk.hlp < prev    next >
Text File  |  1997-05-05  |  65KB  |  1,276 lines

  1. ! Gawk.Hlp
  2. !                                                       Pat Rankin, Jun'90
  3. !                                                          revised, Jun'91
  4. !                                                          revised, Jul'92
  5. !                                                          revised, Jan'95
  6. !                                                          revised, Apr'97
  7. !   Online help for GAWK.
  8. !
  9. 1 GAWK
  10.  GAWK is GNU awk, the Free Software Foundation's implementation of
  11.  the awk programming language.  awk is an interpretive language which
  12.  can handle many data-reformatting jobs with just a few lines of code.
  13.  It has powerful string manipulation and pattern matching capabilities
  14.  built in.  This version should be compatible with POSIX 1003.2 awk.
  15.  
  16.  The VMS version of GAWK supports both the original UN*X-style command
  17.  interface and a DCL interface.  The only setup requirement for GAWK
  18.  is to define it as a 'foreign' command:  a DCL symbol with a value
  19.  which begins with '$'.
  20.        $ GAWK :== $disk:[directory]GAWK
  21. 2 GNU_syntax
  22.  GAWK's UN*X-style interface uses the 'dash' convention for specifying
  23.  options and uses spaces to separate multiple arguments.
  24.  
  25.  There are two main alternatives, depending on how the awk program is
  26.  to be passed to GAWK.  Both alternatives share most options.
  27.  
  28.  Usage: $ gawk [-W opts] [-F fs] [-v var=val] -f progfile [--] file ...
  29.     or  $ gawk [-W opts] [-F fs] [-v var=val] [--] "program" file ...
  30.  
  31.  The options are case-sensitive.  On VMS, the DCL command interpreter
  32.  converts unquoted text into uppercase before passing it to the running
  33.  program.  However, GAWK is written in 'C' and the C Run-Time Library
  34.  (VAXCRTL) converts unquoted text into *lowercase*.  Therefore, the
  35.  -Fval and -W options must be enclosed in quotes.
  36.  
  37.  Note:  under VMS POSIX, the usual shell command line processing occurs.
  38. 3 options
  39.  -f file    use the specified file as the awk program source; if more
  40.             than one instance of -f is used, each file will be read
  41.             in succession
  42.  -Fstring   define a value for the FS variable (field separator)
  43.  -v var=val assign a value of 'val' to the variable 'var'
  44.  -W 'options'  additional gawk-specific options; multiple values may
  45.             be separated by commas, or by spaces if they're quoted,
  46.             or mulitple occurrences of -W may be used.
  47.  -W compat  use awk "compatibility mode" to disable GAWK extensions
  48.             and get the behavior of UN*X awk.
  49.  -W copyright [or -W copyleft]  display an abbreviated version of
  50.             the GNU copyright information
  51.  -W help    list command line options (same as -W usage)
  52.  -W lint    warn about suspect or non-portable awk program code
  53.  -W lint-old  warn about constructs not available in original awk
  54.  -W posix   compatibility mode with additional restrictions
  55.  -W re-interval  evaluate '{' and '}' as intervals in regular expressions
  56.  -W traditional  suppress POSIX and GNU regular expression extensions
  57.  -W usage   list command line options (same as -W help)
  58.  -W version display program version number
  59.  --         don't check further arguments for leading dash
  60. 3 program_text
  61.  If the '-f file' option is not used on the command line, then the
  62.  first "non-dash" argument is assumed to be a string of text containing
  63.  the awk source program.  Here is a complete sample program:
  64.        $ gawk -- "BEGIN {print ""\nHello, World!\n""}"
  65.  This program would print a blank line (based on first "\n"), followed
  66.  by a line reading "Hello, World!", followed by another blank line
  67.  (since awk's 'print' statement includes the trailing 'newline').
  68.  
  69.  On VMS, to include a quote character inside of a quoted string, two
  70.  successive quotes ("") must be used.  (Not necessary for VMS POSIX.)
  71. 3 data_files
  72.  After all dash-options are examined, and after the program text if
  73.  there were no occurrences of the -f option, remaining (space separated)
  74.  command line arguments are considered to be data files for the awk
  75.  program to process.  If any of these actually contains an equals sign
  76.  (=), then it is interpreted as a variable assignment instead of a data
  77.  file.  The syntax is 'variable_name=value'.  For example, the command
  78.        $ gawk -f myprog.awk infile.one flag=2 start=0 infile.two
  79.  would read file 'infile.one' for the program in 'myprog.awk', then it
  80.  would set 'flag' to 2 and 'start' to 0, and finally it would read file
  81.  'infile.two' for the program.  Note that in a case like this, the two
  82.  assignments actually occur after the first file has been processed,
  83.  not at program startup when the command line is first scanned.
  84. 3 IO_redirection
  85.  The command parsing in the VMS implementation of GAWK does some
  86.  emulation of a UN*X-style shell, where certain characters on the
  87.  command line have special meaning.  In particular, the symbols '<',
  88.  '>', '|', '*', and '?' receive special handling before the main part
  89.  of the program has a chance to see them.  The symbols '<' and '>'
  90.  perform some file manipulation from the command line:
  91.  
  92.  <ifile     open file 'ifile' (readonly) as 'stdin' [SYS$INPUT]
  93.  >nfile     create 'nfile' as 'stdout' [SYS$OUTPUT], in stream-lf format
  94.  >>ofile    append to 'ofile' for 'stdout'; create it if necessary
  95.  >&efile    point 'stderr' [SYS$ERROR] at 'efile', but don't open it yet
  96.  >$vfile    create 'vfile' as 'stdout', using RMS attributes appropriate
  97.             for a standard text file (variable length records with
  98.             implied carriage control)
  99.  >+bfile    create 'bfile' as 'stdout' using binary mode
  100.  2>&1       route error messages into the regular output stream
  101.  1>&2       send output data to the error destination
  102.  <<sentinel error; reading stdin until 'sentinel' not supported
  103.  <-, >-     error; closure of stdin or stdout from cmd line not supported
  104.  >>$vfile   incorrect; would be interpreted as file "$vfile" in stream-lf
  105.             format rather than as file "vfile" in RMS 'text' format
  106.  |          error; command line pipes not supported
  107.  
  108.  Note:  under VMS POSIX these features are implemented by the shell
  109.  rather than inside GAWK, so consult the shell documentation for
  110.  specific details.
  111. 3 wildcard_expansion
  112.  The command parsing in the VMS implementation of GAWK does some
  113.  emulation of a UN*X-style shell, where certain characters on the
  114.  command line have special meaning.  In particular, the symbols '<',
  115.  '>', '*', '%', and '?' receive special handling before the main part
  116.  of the program has a chance to see them.  The symbols '*', '%' and '?'
  117.  are used as wildcards in filenames.  '*' and '%' have their usual VMS
  118.  meanings of multiple character and single character wildcards,
  119.  respectively, and '?' is also treated as a single character wildcard.
  120.  Wildcard expansion only works for filenames specified in native VMS
  121.  filename syntax (eg, "[-.sibling]*"), not for ones specified pseudo-
  122.  Unix syntax (eg, "../sibling/*").
  123.  
  124.  When a command line argument that should be a filename contains any
  125.  of the wildcard characters, a directory lookup is attempted for files
  126.  which match the specified pattern.  If one or more matching files are
  127.  found, those filenames are put into the command line in place of the
  128.  original pattern.  If no matching files are found, the original
  129.  pattern is left in place.
  130.  
  131.  Note:  under VMS POSIX wildcard expansion, or "file globbing", is
  132.  performed by the shell rather than inside GAWK, so consult the shell
  133.  documentation for details.  In particular, the last sentence of the
  134.  previous paragraph does not apply.
  135. 2 DCL_syntax
  136.  GAWK's DCL-style interface is more or less a standard DCL command, with
  137.  one required parameter.  Multiple values--when present--are separated
  138.  by commas.
  139.  
  140.  There are two main alternatives, depending on how the awk program is
  141.  to be passed to GAWK.  Both alternatives share most options.
  142.  
  143.  Usage:  GAWK  /COMMANDS="awk program text"  data_file[,data_file,...]
  144.     or   GAWK  /INPUT=awk_file  data_file[,"Var=value",data_file,...]
  145.  (  or   GAWK  /INPUT=(awk_file1,awk_file2,...)  data_file[,...]       )
  146.  
  147.  Not applicable under VMS POSIX.
  148. 3 Parameter
  149.  data_file[,datafile,...]       (data_file data_file ...)
  150.  data_file[,"Var=value",...,data_file,...]      (data_file Var=value &c)
  151.  
  152.   Data file(s) for the awk program to process.  If any of these
  153.   actually contains an equals sign (=), then it is interpreted as
  154.   a variable assignment instead of a data file.  The syntax is
  155.   "variable_name=value".  Quotes are required for non-file parameters.
  156.  
  157.   For example, the command
  158.        $ gawk/input=myprog.awk infile.one,"flag=2","start=0",infile.two
  159.   would read file 'infile.one' for the program in 'myprog.awk', then it
  160.   would set 'flag' to 2 and 'start' to 0, and finally it would read file
  161.   'infile.two' for the program.  Note that in a case like this, the two
  162.   assignments actually occur after the first file has been processed,
  163.   not at program startup when the command line is first scanned.
  164.  
  165.   Wildcard file lookups are attempted on data file specifications.  See
  166.   subtopic 'GAWK GNU_syntax wildcard_expansion' for details.
  167.  
  168.   At least one data_file parameter value is required.  An exception is
  169.   made if /usage, /version, or /copyright is specified *and* if GAWK is
  170.   defined as a 'foreign' command rather than a 'native' DCL command.
  171. 3 Qualifiers
  172. /COMMANDS
  173.  /COMMANDS="awk program text"   (-- "awk program text")
  174.  
  175.   For short programs, it is possible to include the complete program
  176.   on the command line.  The quotes are required.  Here is a complete
  177.   sample program:
  178.        $ gawk/commands="BEGIN {print ""\nHello, World!\n""}" NL:
  179.   This program would print a blank line (based on first "\n"), followed
  180.   by a line reading "Hello, World!", followed by another blank line
  181.   (since awk's 'print' statement includes the trailing 'newline').
  182.  
  183.   To include a quote character inside of a quoted string, two
  184.   successive quotes ("") must be used.
  185.  
  186.   Either /COMMANDS or /INPUT (but not both) must be supplied.
  187. /INPUT
  188.  /INPUT=(awk_file1,awk_file2)   (-f awk_file1 -f awk_file2)
  189.  
  190.   Used to specify one or more files containing the source code of
  191.   the awk program.  If more than one file is used, separate them
  192.   with commas and enclose the list in parentheses.
  193.  
  194.   Multiple source files are processed in order as if they had been
  195.   concatenated together.
  196.  
  197.   Either /INPUT or /COMMANDS (but not both) must be supplied.
  198. /FIELD_SEPARATOR
  199.  /FIELD_SEPARATOR="FS_value"    (-F"FS_value")
  200.  
  201.   Assign a value to the built in variable FS (field separator).
  202. /VARIABLES
  203.  /VARIABLES=("Var1=val1","Var2=val2",...)  (-v Var1=val1 -v Var2=val2)
  204.  
  205.   Assign value(s) to the specified variable(s).
  206. /REG_EXPR
  207.  /REG_EXPR={AWK | EGREP | POSIX}   (-a vs -e options [obsolete])
  208.  
  209.   This qualifier is obsolete and has no effect.
  210. /STRICT
  211.  /[NO]STRICT            (-"W compat" option)
  212.  
  213.   Use strict awk compatibility mode (/strict) and suppress GAWK
  214.   extensions.  The default is /NOSTRICT.
  215. /POSIX
  216.  /[NO]POSIX             (-"W posix" option)
  217.  
  218.   Use POSIX compatibility mode (/posix) and suppress GAWK extensions.
  219.   The default is /NOPOSIX.  Slightly more restrictive than /strict.
  220. /LINT
  221.  /[NO]LINT              (-"W lint" option)
  222.  
  223.   Check the awk program cafefully for potential problems that might
  224.   be encountered if it were to be used with other awk implementations,
  225.   and print warnings for anything found.  The default in /NOLINT.
  226. /VERSION
  227.  /VERSION               (-"W version" option)
  228.  
  229.   Print GAWK's version number.
  230. /COPYRIGHT
  231.  /COPYRIGHT             (-"W copyright" or -"W copyleft" option)
  232.  
  233.   Print a brief version of GAWK's copyright notice.
  234. /USAGE
  235.  /USAGE                 (comparable to -"W usage" or -"W help" option)
  236.  
  237.   Print a compact summary of the command line options.
  238.  
  239.   After the 'usage' message is printed, GAWK terminates regardless
  240.   of any other command line options.
  241. /OUTPUT
  242.  /OUTPUT=out_file       (>$out_file)
  243.  
  244.   Write program output into 'out_file'.  The default is SYS$OUTPUT.
  245. 2 awk_language
  246.  An awk program consists of one or more pattern-action pairs, sometimes
  247.  referred to as "rules".  For each record of an input (data) file, the
  248.  rules are checked sequentially.  Any pattern which matches the input
  249.  record triggers that rule's action.  Actions are instructions which
  250.  resemble statements in the 'C' programming language.  Patterns come
  251.  in several varieties, including field comparisons, regular expression
  252.  matching, and special cases defined by reserved keywords.
  253.  
  254.  All awk keywords and variables are case-sensitive.  Text matching is
  255.  also sensitive to character case unless the builtin variable IGNORECASE
  256.  is set to a non-zero value.
  257. 3 rules
  258.  The syntax for a pattern-action 'rule' is simply
  259.        PATTERN { ACTION }
  260.  where the braces ({}) are required punctuation for the action.
  261.  Semicolons (;) or 'newlines' (ie, having the text on a separate line)
  262.  delimit multiple rules and also multiple actions within a given rule.
  263.  Either the pattern or the action may be omitted; an empty pattern
  264.  matches every record of the input file; a missing action (not an empty
  265.  action inside of braces), is an implicit request to print the current
  266.  record; an empty action (ie, {}) is legal but not very useful.
  267. 3 patterns
  268.  There are several types of patterns available for awk rules.
  269.  
  270.   expression  an 'expression' is something to be evaluated (perhaps
  271.                          a comparison or function call) which will
  272.                          be considered true if non-zero (for numeric
  273.                          results) or if non-null (for strings)
  274.   /regular_expression/ slashes (/) delimit a regular expression
  275.                          which is used as a pattern
  276.   pattern1, pattern2   a pair of patterns separated by a comma (,),
  277.                          which causes a range of records to trigger
  278.                          the associated action; the records which
  279.                          match the patterns are included in the range
  280.   <null>      an omitted pattern (in this text, the  string '<null>'
  281.                          is displayed, but in an awk program, it
  282.                          would really be blank) matches every record
  283.   BEGIN       keyword for specifying a rule to be executed prior to
  284.                          reading the 1st record of the 1st input file
  285.   END         keyword for specifying a rule to be executed after
  286.                          handling the last input record of last file
  287. 4 examples
  288.  Some example patterns (mostly with the corresponding actions omitted)
  289.  
  290.  NF > 0     # comparison expression:  matches non-null records
  291.  $0         # implied comparison:  also matches non-null records
  292.  $2 > 1000 && sum <= 999999     # slightly more elaborate expression
  293.  /x/        # regular expression matching any record with an 'x' in it
  294.  /^ /       # reg-expr matching records beginning with a space
  295.  $1 == "start", $NF == "stop"   # range pattern for input in which
  296.                 some data lines begin with 'start' and/or end with
  297.                 'stop' in order to collect groups of records
  298.         { sum += $1 }   # null pattern:  it's action (add field #1 to
  299.                 variable 'sum') would be executed for every record
  300.  BEGIN  { sum = 0 }     # keyword 'BEGIN':  perform this action before
  301.                 reading the input file (note: initialization to 0 is
  302.                 unnecessary in awk)
  303.  END    { print "total =", sum }    # keyword 'END':  perform this
  304.                 action after the last input record has been processed
  305. 3 actions
  306.  An 'action' is something to do when a given record has matched the
  307.  corresponding pattern in a rule.  In general, actions resemble 'C'
  308.  statements and expressions.  The action in a rule must be enclosed
  309.  in braces ({}).
  310.  
  311.  Each action can contain more than one statement or expression to be
  312.  executed, provided that they're separated by semicolons (;) and/or
  313.  on separate lines.
  314.  
  315.  An omitted action is equivalent to
  316.        { print $0 }
  317.  which prints the current record.
  318. 3 operators
  319.  Relational operators
  320.     ==    compare for equality
  321.     !=    compare for inequality
  322.     <, <=, >, >=  numerical or lexical comparison (less than, less or
  323.                     equal, greater than, greater or equal, respectively)
  324.     ~     match against a regular expression
  325.     !~    match against a regular expression, but accept failed matches
  326.             instead of successful ones
  327.  Arithmetic operators
  328.     +     addition
  329.     -     subtraction
  330.     *     multiplication
  331.     /     division
  332.     %     remainder
  333.     ^, ** exponentiation ('**' is a synonym for '^', unless POSIX
  334.             compatibility is specified, in which case it's invalid)
  335.  Boolean operators (aka Logical operators)
  336.           a value is considered false if it's 0 or a null string,
  337.             it is true otherwise; the result of a boolean operation
  338.             (and also of a comparison operation) will be 0 when false
  339.             or 1 when true
  340.     ||    or [expression (a || b) is true if either a is true or b
  341.             is true or both a and b are true; it is false otherwise;
  342.             b is not evaluated unless a is false (ie, short-circuit)]
  343.     &&    and [expression (a && b) is true if both a and b are true;
  344.             it is false otherwise; b is only evaluated if a is true]
  345.     !     not [expression (!a) is true if a is false, false otherwise]
  346.     in    array membership; the keyword 'in' tests whether the value
  347.             on the left represents a current subscript in the array
  348.             named on the right
  349.  Conditional operator
  350.     ? :   the conditional operator takes three operands; the first is
  351.             an expression to evaluate, the second is the expression to
  352.             use if the first was true, the third is the expression to
  353.             use if it was false [simple example (a < b ? b : a) gives
  354.             the maximum of a and b]
  355.  Assignment operators
  356.     =     store the value on the right into the variable or array slot
  357.             on the left [expression (a = b) stores the value of b in a]
  358.     +=, -=, *=, /=, %=, ^=, **=  perform the indicated arithmetic
  359.            operation using the current value of the variable or array
  360.             element of the left side and the expression on the right
  361.             side, then store the result in the left side
  362.     ++    increment by 1 [expression (++a) gets the current value of
  363.             a and adds 1 to it, stores that back in a, and returns the
  364.             new value; expression (a++) gets the current value of a,
  365.             adds 1 to it, stores that back in a, but returns the
  366.             original value of a]
  367.     --    decrement by 1 (analogous to increment)
  368.  String operators
  369.           there is no explicit operator for string concatenation;
  370.             two values and/or variables side-by-side are implicitly
  371.             concatenated into a string (numeric values are first
  372.             converted into their string equivalents)
  373.  Conversion between numeric and string values
  374.           there is no explicit operator for conversion; adding 0
  375.             to a string with force it to be converted to a number
  376.             (the numeric value will be 0 if the string does not
  377.             represent an integer or floating point number); the
  378.             reverse, converting a number into a string, is done by
  379.             concatenating a null string ("") to it [the expression
  380.             (5.75 "") evaluates to "5.75"]
  381.  Field 'operator'
  382.     $     prefixing a number or variable with a dollar sign ($)
  383.             causes the appropriate record field to be returned [($2)
  384.             gives the second field of the record, ($NF) gives the
  385.             last field (since the builtin variable NF is set to the
  386.             number of fields in the current record)]
  387.  Array subscript operator
  388.     ,     multi-dimensional arrays are simulated by using comma (,)
  389.             separated array indices; the actual index is generated
  390.             by replacing commas with the value of builtin SUBSEP,
  391.             then concatenating the expression into a string index
  392.           [comma is also used to separate arguments in function
  393.             calls and user-defined function definitions]
  394.           [comma is *also* used to indicate a range pattern in an
  395.             awk rule]
  396.  Escape 'operator'
  397.     \     In quoted character strings, the backslash (\) character
  398.             causes the following character to be interpreted in a
  399.             special manner [string "one\ntwo" has an embedded newline
  400.             character (linefeed on VMS, but treated as if it were both
  401.             carriage-return and linefeed); string "\033[" has an ASCII
  402.             'escape' character (which has octal value 033) followed by
  403.             a 'right-bracket' character]
  404.           Backslash is also used in regular expressions
  405.  Redirection operators
  406.     <     Read-from -- valid with 'getline'
  407.     >     Write-to (create new file) -- valid with 'print' and 'printf'
  408.     >>    Append-to (create file if it doesn't already exist)
  409.     |     Pipe-from/to -- valid with 'getline', 'print', and 'printf'
  410. 4 precedence
  411.  Operator precedence, listed from highest to lowest.  Assignment,
  412.  conditional, and exponentiation operators group from right to left;
  413.  all others group from left to right.  Parentheses may be used to
  414.  override the normal order.
  415.  
  416.      field ($)
  417.      increment (++), decrement (--)
  418.      exponentiation (^, **)
  419.      unary plus (+), unary minus (-), boolean not (!)
  420.      multiplication (*), division (/), remainder (%)
  421.      addition (+), subtraction (-)
  422.      concatenation (no special symbol; implied by context)
  423.      relational (==, !=, <, >=, etc), and redirection (<, >, >>, |)
  424.        Relational and redirection operators have the same precedence
  425.        and use similar symbols; context distinguishes between them
  426.      matching (~, !~)
  427.      array membership ('in')
  428.      boolean and (&&)
  429.      boolean or (||)
  430.      conditional (? :)
  431.      assignment (=, +=, etc)
  432. 4 escaped_characters
  433.  Inside of a quoted string or constant regular expression, the
  434.  backslash (\) character gives special meaning to the character(s)
  435.  after it.  Special character letters are case sensitive.
  436.     \\    results in one backslash in the string
  437.     \a    is an 'alert' (<ctrl/G>. the ASCII <bell> character)
  438.     \b    is a backspace (BS, <ctrl/H>)
  439.     \f    is a form feed (FF, <ctrl/L>)
  440.     \n    'newline' (<ctrl/J> [line feed treated as CR+LF]
  441.     \r    carriage return (CR, <ctrl/M> [re-positions at the
  442.             beginning of the current line]
  443.     \t    tab (HT, <ctrl/I>)
  444.     \v    vertical tab (VT, <ctrl/K>)
  445.     \###  is an arbitrary character, where '###' represents 1 to 3
  446.             octal (ie, 0 thru 7) digits
  447.     \x##  is an alternate arbitrary character, where '##' represents
  448.             1 or more hexadecimal (ie, 0 thru 9 and/or A through E
  449.             and/or a through e) digits; if more than two digits
  450.             follow, the result is undefined; not recognized if POSIX
  451.             compatibility mode is specified.
  452.  
  453.  When a regular expression is represented in string form ("regex"
  454.  as opposed to /regex/), backslashes need to be paired.  The first
  455.  one quotes the second during string processing, and the second one
  456.  remains to be used to quote whatever follows in regular expression
  457.  processing.  For example, to match variable `xxx' against a period
  458.  character, use (xxx ~ "\\.") or (xxx ~ /\./); if you tried to use
  459.  (xxx ~ "\."), after string processing it would operate as (xxx ~ /./)
  460.  and end up matching any single character rather than just a period.
  461. 3 statements
  462.  A statement refers to a unit of instruction found in the action
  463.  part of an awk rule, and also found in the definition of a function.
  464.  The distinction between action, statement, and expression usually
  465.  won't matter to an awk programmer.
  466.  
  467.  Compound statements consist of multiple statements separated by
  468.  semicolons or newlines and enclosed within braces ({}).  They are
  469.  sometimes referred to as 'blocks'.
  470. 4 expressions
  471.  An expression such as 'a = 10' or 'n += i++' is a valid statement.
  472.  
  473.  Function invocations such as 'reformat_field($3)' are also valid
  474.  statements.
  475. 4 if-then-else
  476.  A conditional statement in awk uses the same syntax as for the 'C'
  477.  programming language:  the 'if' keyword, followed by an expression
  478.  in parentheses, followed by a statement--or block of statements
  479.  enclosed within braces ({})--which will be executed if the expression
  480.  is true but skipped if it's false.  This can optionally be followed
  481.  by the 'else' keyword and another statement--or block of statements--
  482.  which will be executed if (and only if) the expression was false.
  483. 5 examples
  484.  Simple example showing a statement used to control how many numbers
  485.  are printed on a given line.
  486.        if ( ++i <= 10 )     #check whether this would be the 11th
  487.               printf(" %5d", k)     #print on current line if not
  488.        else {
  489.               printf("\n %5d", k)   #print on next line if so
  490.               i = 1                 #and reset the counter
  491.        }
  492.  Another example ('next' is described under 'action-controls')
  493.        if ($1 > $2) { print "rejected"; next } else diff = $2 - $1
  494. 4 loops
  495.  Three types of loop statements are available in awk.  Each uses
  496.  the same syntax as 'C'.  The simplest of the three is the 'while'
  497.  statement.  It consists of the 'while' keyword, followed by an
  498.  expression enclosed within parentheses, followed by a statement--or
  499.  block of statements in braces ({})--which will be executed if the
  500.  expression evaluates to true.  The expression is evaluated before
  501.  attempting to execute the statement; if it's true, the statement is
  502.  executed (the entire block of statements if there is a block) and
  503.  then the expression is re-evaluated.
  504.  
  505.  The second type of loop is the do-while loop.  It consists of the
  506.  'do' keyword, followed by a statement (usually a block of statements
  507.  enclosed within braces), followed by the 'while' keyword, followed
  508.  by a test expression enclosed within parentheses.  The statement--or
  509.  block--is always executed at least once.  Then the test expression
  510.  is evaluated, and the statement(s) re-executed if the result was
  511.  true (followed by re-evaluation of the test, and so on).
  512.  
  513.  The most complex of the three loops is the 'for' statement, and it
  514.  has a second variant that is not found in 'C'.  The ordinary for-loop
  515.  consists of the 'for' keyword, followed by three semicolon-separated
  516.  expressions enclosed within parentheses, followed by a statement or
  517.  brace-enclosed block of statements.  The first of the three
  518.  expressions is an initialization clause; it is done before starting
  519.  the loop.  The second expression is used as a test, just like the
  520.  expression in a while-loop.  It is checked before attempting to
  521.  execute the statement block, and then re-checked after each execution
  522.  (if any) of the block.  The third expression is an 'increment' clause;
  523.  it is evaluated after an execution of the statement block and before
  524.  re-evaluation of the test (2nd) expression.  Normally, the increment
  525.  clause will change a variable used in the test clause, in such a
  526.  fashion that the test clause will eventually evaluate to false and
  527.  cause the loop to finish.
  528.  
  529.  Note to 'C' programmers:  the comma (,) operator commonly used in
  530.  'C' for-loop expressions is not valid in awk.
  531.  
  532.  The awk-specific variant of the for-loop is used for processing
  533.  arrays.  Its syntax is 'for' keyword, followed by variable_name 'in'
  534.  array_name (where 'var in array' is enclosed in parentheses),
  535.  followed by a statement (or block).  Each valid subscript value for
  536.  the array in question is successively placed--in no particular
  537.  order--into the specified 'index' variable.
  538. 5 while_example
  539.  # strip fields from the input record until there's nothing left
  540.  while (NF > 0) {
  541.      $1 = ""    #this will affect the value of $0
  542.      $0 = $0    #this causes $0 and NF to be re-evaluated
  543.      print
  544.  }
  545. 5 do_while_example
  546.  # This is a variation of the while_example; it gives a slightly
  547.  #   different display due to the order of operation.
  548.  # echo input record until all fields have been stripped
  549.  do {
  550.      print      #output $0
  551.      $1 = ""    #this will affect the value of $0
  552.      $0 = $0    #this causes $0 and NF to be re-evaluated
  553.  } while (NF > 0)
  554. 5 for_example
  555.  # echo command line arguments (won't include option switches)
  556.  for ( i = 0; i < ARGC; i++ )  print ARGV[i]
  557.  
  558.  # display contents of builtin environment array
  559.  for (itm in ENVIRON)
  560.      print itm, ENVIRON[itm]
  561. 4 loop-controls
  562.  There are two special statements--both from 'C'--for changing the
  563.  behavior of loop execution.  The 'continue' statement is useful in
  564.  a compound (block) statement; when executed, it effectively skips
  565.  the rest of the block so that the increment-expression (only for
  566.  for-loops) and loop-termination expression can be re-evaluated.
  567.  
  568.  The 'break' statement, when executed, effectively skips the rest
  569.  of the block and also treats the test expression as if it were
  570.  false (instead of actually re-evaluating it).  In this case, the
  571.  increment-expression of a for-loop is also skipped.
  572.  
  573.  Inside nested loops, both 'break' and 'continue' only apply to the
  574.  innermost loop.  When in compatibility mode, 'break' or 'continue'
  575.  may be used outside of a loop; either will be treated like 'next'
  576.  (see action-controls).
  577. 4 action-controls
  578.  There are two special statements for controlling statement execution.
  579.  The 'next' statement, when executed, causes the rest of the current
  580.  action and all further pattern-action rules to be skipped, so that
  581.  the next input record will be immediately processed.  This is useful
  582.  if any early action knows that the current record will fail all the
  583.  remaining patterns; skipping those rules will reduce processing time.
  584.  An extended form, 'next file', is also available.  It causes the
  585.  remainder of the current file to be skipped, and then either the
  586.  next input file will be processed, if any, or the END action will be
  587.  performed.  'next file' is not available in traditional awk.
  588.  
  589.  The 'exit' statement causes GAWK execution to terminate.  All open
  590.  files are closed, and no further processing is done.  The END rule,
  591.  if any, is executed.  'exit' takes an optional numeric value as a
  592.  argument which is used as an exit status value, so that some sort
  593.  of indication of why execution has stopped can be passed on to the
  594.  user's environment.
  595. 4 other_statements
  596.  The delete statement is used to remove an element from an array.
  597.  The syntax is 'delete' keyword followed by array name, followed
  598.  by index value enclosed in square brackets ([]).  'delete' may
  599.  also used on an array name, without any index specified, to delete
  600.  all its elements in a single operation.
  601.  
  602.  The return statement is used in user-defined functions.  The syntax
  603.  is the keyword 'return' optionally followed by a string or numeric
  604.  expression.
  605.  
  606.  See also subtopic 'functions IO_functions' for a description of
  607.  'print', 'printf', and 'getline'.
  608. 3 fields
  609.  When an input record is read, it is automatically split into fields
  610.  based on the current values of FS (builtin variable defining field
  611.  separator expression) and RS (builtin variable defining record
  612.  separator character).  The default value of FS is an expression
  613.  which matches one or more spaces and tabs; the default for RS is
  614.  newline.  If the FIELDWIDTHS variable is set to a space separated
  615.  list of numbers (as in ``FIELDWIDTHS = "2 3 2"'') then the input
  616.  is treated as if it had fixed-width fields of the indicated sizes
  617.  and the FS value will be ignored.
  618.  
  619.  The field prefix operator ($), is used to reference a particular
  620.  field.  For example, $3 designates the third field of the current
  621.  record.  The entire record can be referenced via $0 (and it holds
  622.  the actual input record, not the values of $1, $2, ... concatenated
  623.  together, so multiple spaces--when present--remain intact, unless
  624.  a new value gets assigned).
  625.  
  626.  The builtin variable NF holds the number of fields in the current
  627.  record.  $NF is therefore the value of the last field.  Attempts to
  628.  access fields beyond NF result in null values (if a record contained
  629.  3 fields, the value of $5 would be "").
  630.  
  631.  Assigning a new value to $0 causes all the other field values (and NF)
  632.  to be re-evaluated.  Changing a specific field will cause $0 to receive
  633.  a new value once it's re-evaluated, but until then the other existing
  634.  fields remain unchanged.
  635. 3 variables
  636.  Variables in awk can hold both numeric and string values and do not
  637.  have to be pre-declared.  In fact, there is no way to explicitly
  638.  declare them at all.  Variable names consist of a leading letter
  639.  (either upper or lower case, which are distinct from each other)
  640.  or underscore (_) character followed by any number of letters,
  641.  digits, or underscores.
  642.  
  643.  When a variable that didn't previously exist is referenced, it is
  644.  created and given a null value.  A null value is treated as 0 when
  645.  used as a number, and is a string of zero characters in length if
  646.  used as a string.
  647. 4 builtin_variables
  648.  GAWK maintains several 'built-in' variables.  All have default values;
  649.  some are updated automatically.  All the builtins have uppercase-only
  650.  names.
  651.  
  652.  These builtin variables control how awk behaves
  653.    FS  input field separator; default is a single space, which is
  654.          treated as if it were a regular expression for matching
  655.          one or more spaces and/or tabs; a value of " " also has a
  656.          second special-case side-effect of causing leading blanks
  657.          to be ignored instead of producing a null first field;
  658.          initial value can be specified on the command line with
  659.          the -F option (or /field_separator); the value can be a
  660.          regular expression
  661.    RS  input record separator; default value is a newline ("\n");
  662.          the value can be multiple characters or a regular expression
  663.    OFS output field separator; value to place between variables in
  664.          a 'print' statement; default is one space; can be arbitrary
  665.          string
  666.    ORS output record separator; value to implicitly terminate 'print'
  667.          statement with; default is newline ("\n"); can be arbitrary
  668.          string
  669.    OFMT default output format used for printing numbers; default
  670.          value is "%.6g"
  671.    CONVFMT conversion format used for number-to-string conversions;
  672.          default value is also "%.6g", like OFMT; not used when the
  673.          number has a value which may be represented internally as
  674.          an exact integer (typically within -2147483648 to 2147483647)
  675.    SUBSEP subscript separator for array indices; used when an array
  676.          subscript is specified as a comma separated list of values:
  677.          the comma is replaced by SUBSEP and the resulting index
  678.          is a concatenation of the values and SUBSEP(s); default
  679.          value is "\034"; value may be arbitrary string
  680.    IGNORECASE string and regular expression matching flag; if true
  681.          (non-zero) matching ignores differences between upper and
  682.          lower case letters; affects the '~' and '!~' operators,
  683.          the 'index', 'match', 'split', 'sub', and 'gsub' functions,
  684.          and field splitting based on FS; default value is false (0);
  685.          has no effect if GAWK is in strict compatibility mode
  686.    FIELDWIDTHS space or tab separated list of width sizes; takes
  687.          precedence over FS when set, but is cleared if FS has a
  688.          value assigned to it; [note: the current implementation
  689.          of fixed-field input is considered experimental and is
  690.          expected to evolve over time]
  691.  
  692.  These builtin variables provide useful information
  693.    NF  number of fields in the current record
  694.    NR  record number (accumulated over all files when more than one
  695.          input file is processed by the same program)
  696.    FNR current record number of the current input file; reset to 0
  697.          each time an input file is completed
  698.    RT  record terminator, the input text which matched RS; not
  699.          available when the `-W traditional' option is used
  700.    RSTART starting position of substring matched by last invocation
  701.          of the 'match' function; set to 0 if a match fails and at
  702.          the start of each input record
  703.    RLENGTH length of substring matched by the last invocation of the
  704.          'match' function; set to -1 if a match fails
  705.    FILENAME name of the input file currently being processed; the
  706.          special name "-" is used to represent the standard input
  707.    ENVIRON array of miscellaneous user environment values; the VMS
  708.          implementation of GAWK provides values for ["USER"] (the
  709.          username), ["PATH"] (current default directory), ["HOME"]
  710.          (the user's login directory), and "[TERM]" (terminal type
  711.          if available) [all info provided by C RTL's environ]
  712.    ERRNO information about the cause of failure for 'getline' or
  713.          'close'; "0" if no such failure has occured.
  714.    ARGC number of elements in the ARGV array, counting [0] which is
  715.          the program name (ie, "gawk")
  716.    ARGV array of command-line arguments (in [0] to [ARGC-1]); the
  717.          program name (ie, "gawk") in held in ARGV[0]; command line
  718.          parameters (data files and "var=value" expressions, but not
  719.          program options or the awk program text string if present)
  720.          are stored in ARGV[1] through ARGV[ARGC-1]; the awk program
  721.          can change values of ARGC and ARGV[] during execution in
  722.          order to alter which files are processed or which between-
  723.          file assignments are made
  724.    ARGIND current index into ARGV[]
  725. 4 arrays
  726.  awk supports associative arrays to collect data into tables.  Array
  727.  elements can be either numeric or string, as can the indices used to
  728.  access them.  Each array must have a unique name, but a given array
  729.  can hold both string and numeric elements at the same time.  Arrays
  730.  are one-dimensional only, but multi-dimensional arrays can be
  731.  simulated using comma (,) separated indices, whereby a single index
  732.  value gets created by replacing commas with SUBSEP and concatenating
  733.  the resulting expression into a single string.
  734.  
  735.  Referencing an array element is done with the expression
  736.        Array[Index]
  737.  where 'Array' represents the array's name and 'Index' represents a
  738.  value or expression used for a subscript.  If the requested array
  739.  element did not exist, it will be created and assigned an initial
  740.  null value.  To check whether an element exists without creating it,
  741.  use the 'in' boolean operator.
  742.        Index in Array
  743.  would check 'Array' for element 'Index' and return 1 if it existed
  744.  or 0 otherwise.  To remove an element from an array, use the 'delete'
  745.  statement
  746.        delete Array[Index]
  747.  To remove all array elements at once, use
  748.        delete Array
  749.  Note:  the latter is a gawk extension; also, there is no way to
  750.  delete an ordinary variable or an entire array; 'delete' only works
  751.  on array elements.
  752.  
  753.  To process all elements of an array (in succession) when their
  754.  subscripts might be unknown, use the 'in' variant of the for-loop
  755.        for (Index in Array) { ... }
  756. 3 functions
  757.  awk supports both built-in and user-defined functions.  A function
  758.  may be considered a 'black-box' which accepts zero or more input
  759.  parameters, performs some calculations or other manipulations based
  760.  on them, and returns a single result.
  761.  
  762.  The syntax for calling a function consists of the function name
  763.  immediately followed by an open parenthesis (left parenthesis '('),
  764.  followed by an argument list, followed by a closing parenthesis
  765.  (right parenthesis ')').  The argument list is a sequence of values
  766.  (numbers, strings, variables, array references, or expressions
  767.  involving the above and/or nested function calls), separated by
  768.  commas and optional white space.
  769.  
  770.  The parentheses are required punctuation, except for the 'print' and
  771.  'printf' builtin IO functions, where they're optional, and for the
  772.  builtin IO function 'getline', where they're not allowed.  Some
  773.  functions support optional [trailing] arguments which can be simply
  774.  omitted (along with the corresponding comma if applicable).
  775. 4 numeric_functions
  776.  Builtin numeric functions
  777.    int(n)      returns the value of 'n' with any fraction truncated
  778.                  [truncation of negative values is towards 0]
  779.    sqrt(n)     the square root of n
  780.    exp(n)      the exponential of n ('e' raised to the 'n'th power)
  781.    log(n)      natural logarithm of n
  782.    sin(n)      sine of n (in radians)
  783.    cos(n)      cosine of n (radians)
  784.    atan2(m,n)  arctangent of m/n (radians)
  785.    rand()      random number in the range 0 to 1 (exclusive)
  786.    srand(s)    sets the random number 'seed' to s, so that a sequence
  787.                  of 'random' numbers can be repeated; returns the
  788.                  previous seed value; srand() [argument omitted] sets
  789.                  the seed to an 'unpredictable' value (based on date
  790.                  and time, for instance, so should be unrepeatable)
  791. 4 string_functions
  792.  Builtin string functions
  793.    index(s,t)  search string s for substring t; result is 1-based
  794.                  offset of t within s, or 0 if not found
  795.    length(s)   returns the length of string s; either 'length()'
  796.                  with its argument omitted or 'length' without any
  797.                  parenthesized argument list will return length of $0
  798.    match(s,r)  search string s for regular expression r; the offset
  799.                  of the longest, left-most substring which matches
  800.                  is returned, or 0 if no match was found; the builtin
  801.                  variables RSTART and RLENGTH are also set [RSTART to
  802.                  the return value and RLENGTH to the size of the
  803.                  matching substring, or to -1 if no match was found]
  804.    split(s,a,f) break string s into components based on field
  805.                  separator f and store them in array a (into elements
  806.                  [1], [2], and so on); the last argument is optional,
  807.                  if omitted, the value of FS is used; the return value
  808.                  is the number of components found
  809.    sprintf(f,e,...) format expression(s) e using format string f and
  810.                  return the result as a string; formatting is similar
  811.                  to the printf function
  812.    sub(r,t,s)  search string target s for regular expression r, and
  813.                  if a match is found, replace the matching text with
  814.                  substring t, then store the result back in s; if s
  815.                  is omitted, use $0 for the string; the result is
  816.                  either 1 if a match+substitution was made, or 0
  817.                  otherwise; if substring t contains the character
  818.                  '&', the text which matched the regular expression
  819.                  is used instead of '&' [to suppress this feature
  820.                  of '&', 'quote' it with a backslash (\); since this
  821.                  will be inside a quoted string which will receive
  822.                  'backslash' processing before being passed to sub(),
  823.                  *two* consecutive backslashes will be needed "\\&"]
  824.    gsub(r,t,s) similar to sub(), but gsub() replaces all nonoverlapping
  825.                  substrings instead of just the first, and the return
  826.                  value is the number of substitutions made
  827.    gensub(r,t,n,s) search string s ($0 if omitted) for regexp r and
  828.                  replace the n'th occurrence with substring t; the
  829.                  result is the new string and s (or $0) remains
  830.                  unchanged; if n begins with letter "g" or "G" then
  831.                  all matches are replaced instead of just the n'th;
  832.                  if r has parenthesized subexpressions in it, t may
  833.                  contain the special sequences \\0, \\1, through \\9
  834.                  which expand into the value of the corresponding
  835.                  subexpression; this function is a gawk extension
  836.    substr(s,p,l) extract a substring l characters long starting at
  837.                  offset p in string s; l is optional, if omitted then
  838.                  the remainder of the string (p thru end) is returned
  839.    tolower(s)  return a copy of string s in which every uppercase
  840.                  letter has been converted into lowercase
  841.    toupper(s)  analogous to tolower(); convert lowercase to uppercase
  842. 4 time_functions
  843.  Builtin time functions
  844.    systime()   return the current time of day as the number of seconds
  845.                  since some reference point; on VMS the reference point
  846.                  is January 1, 1970, at 12 AM local time (not UTC)
  847.    strftime(f,t) format time value t using format f; if t is omitted,
  848.                  the default is systime()
  849. 5 time_formats
  850.  Formatting directives similar to the 'printf' & 'sprintf' functions
  851.  (each is introduced in the format string by preceding it with a
  852.  percent sign (%)); the directive is substituted by the corresponding
  853.  value
  854.    a   abbreviated weekday name (Sun,Mon,Tue,Wed,Thu,Fri,Sat)
  855.    A   full weekday name
  856.    b   abbreviated month name (Jan,Feb,...)
  857.    B   full month name
  858.    c   date and time (Unix-style "aaa bbb dd HH:MM:SS YYYY" format)
  859.    C   century prefix (19 or 20) [not century number, ie 20th]
  860.    d   day of month as two digit decimal number (01-31)
  861.    D   date in mm/dd/yy format
  862.    e   day of month with leading space instead of leading 0 ( 1-31)
  863.    E   ignored; following format character used
  864.    H   hour (24 hour clock) as two digit number (00-23)
  865.    h   abbreviated month name (Jan,Feb,...) [same as %b]
  866.    I   hour (12 hour clock) as two digit number (01-12)
  867.    j   day of year as three digit number (001-366)
  868.    m   month as two digit number (01-12)
  869.    M   minute as two digit number (00-59)
  870.    n   'newline' (ie, treat %n as \n)
  871.    O   ignored; following format character used
  872.    p   AM/PM designation for 12 hour clock
  873.    r   time in AM/PM format ("II:MM:SS p")
  874.    R   time without seconds ("HH:MM")
  875.    S   second as two digit number (00-59)
  876.    t   tab (ie, treat %t as \t)
  877.    T   time ("HH:MM:SS")
  878.    U   week of year (00-53) [first Sunday is first day of week 1]
  879.    V   date (VMS-style "dd-bbb-YYYY" with 'bbb' forced to uppercase)
  880.    w   weekday as decimal digit (0 [Sunday] through 6 [Saturday])
  881.    W   week of year (00-53) [first _Monday_ is first day of week 1]
  882.    x   date ("aaa bbb dd YYYY")
  883.    X   time ("HH:MM:SS")
  884.    y   year without century (00-99)
  885.    Y   year with century (19yy-20yy)
  886.    Z   time zone name (always "local" for VMS)
  887.    %   literal percent sign (%)
  888. 4 IO_functions
  889.  Builtin I/O functions
  890.    print x,... print the values of one or more expressions; if none
  891.                  are listed, $0 is used; parentheses are optional;
  892.                  when multiple values are printed, the current value
  893.                  of builtin OFS (default is 1 space) is used to
  894.                  separate them; the print line is implicitly
  895.                  terminated with the current value of ORS (default
  896.                  is newline); print does not have a return value
  897.    printf(f,x,...) print the values of one or more expressions, using
  898.                  the specified format string; null strings are used
  899.                  to supply missing values (if any); no between field
  900.                  or trailing newline characters are printed, they
  901.                  should be specified within the format string; the
  902.                  argument-enclosing parentheses are optional;
  903.                  printf does not have a return value
  904.    getline v   read a record into variable v; if v is omitted, $0 is
  905.                  used (and NF, NR, and FNR are updated); if v is
  906.                  specified, then field-splitting won't be performed;
  907.                  note:  parentheses around the argument are *not*
  908.                  allowed; return value is 1 for successful read, 0
  909.                  if end of file is encountered, or -1 if some sort
  910.                  of error occurred; [see 'redirection' for several
  911.                  variants]
  912.    close(s)    close a file or pipe specified by the string s; the
  913.                  string used should have the same value as the one
  914.                  used in a getline or print/printf redirection
  915.    fflush(s)   flush output stream s; if s is omitted, stdout is
  916.                  flushed; if it is specified but its value is an
  917.                  empty string, all output streams are flushed
  918.    system(s)   pass string s to executed by the operating system;
  919.                  the command string is executed in a subprocess
  920. 5 redirection
  921.  Both getline and print/printf support variant forms which use
  922.  redirection and pipes.
  923.  
  924.  To read from a file (instead of from the primary input file), use
  925.      getline var < "file"
  926.  or  getline < "file"    (read into $0)
  927.  where the string "file" represents either an actual file name (in
  928.  quotes) or a variable which contains a file name string value or an
  929.  expression which evaluates to a string filename.
  930.  
  931.  To create a pipe executing some command and read the result into
  932.  a variable (or into $0), use
  933.      "command" | getline var
  934.  or  "command" | getline    (read into $0)
  935.  where "command" is a literal string containing an operating system
  936.  command or a variable with a string value representing such a
  937.  command.
  938.  
  939.  To output into a file other that the primary output, use
  940.      print x,... > "file"    (or >> "file")
  941.  or  printf(f,x,...) > "file"    (or >> "file")
  942.  similar to the 'getline' example above.  '>>' causes output to be
  943.  appended to an existing file if it exists, or create the file if
  944.  it doesn't already exist.  '>' always creates a new file.  The
  945.  alternate redirection method of '>$' (for RMS text file attributes)
  946.  is *only* available on the command line, not with 'print' or
  947.  'printf' in  the current release.
  948.  
  949.  To output an error message, use 'print' or 'printf' and redirect
  950.  the output to file "/dev/stderr" (or equivalently to "SYS$ERROR:"
  951.  on VMS).  'stderr' will normally be the user's terminal, even if
  952.  ordinary output is being redirected into a file.
  953.  
  954.  To feed awk output into another command, use
  955.      print x,... | "command"    (similarly for 'printf')
  956.  similar to the second 'getline' example.  In this case, output
  957.  from awk will be passed as input to the specified operating system
  958.  command.  The command must be capable of reading input from 'stdin'
  959.  ("SYS$INPUT:" on VMS) in order to receive data in this manner.
  960.  
  961.  The 'close' function operates on the "file" or "command" argument
  962.  specified here (either a literal string or a variable or expression
  963.  resulting in a string value).  It completely closes the file or
  964.  pipe so that further references to the same file or command string
  965.  would re-open that file or command at the beginning.  Closing a
  966.  pipe or redirection also releases some file-oriented resources.
  967.  
  968.  Note:  the VMS implementation of GAWK uses temporary files to
  969.  simulate pipes, so a command must finish before 'getline' can get
  970.  any input from it, and 'close' must be called for an output pipe
  971.  before any data can be passed to the specified command.
  972. 5 formats
  973.  Formatting characters used by the 'printf' and 'sprintf' functions
  974.  (each is introduced in the format string by preceding it with a
  975.  percent sign (%))
  976.    %   include a literal percent sign (%) in the result
  977.    c   format the next argument as a single ASCII character
  978.          (prints first character of string argument, or corresponding
  979.          ASCII character if numeric argument, e.g. 65 is 'A')
  980.    s   format the next argument as a string (numeric arguments are
  981.          converted into strings on demand)
  982.    d   decimal number (ie, integer value in base 10)
  983.    i   integer (equivalent to decimal)
  984.    o   octal number (integer in base 8)
  985.    x   hexadecimal number (integer in base 16) [lowercase]
  986.    X   hexadecimal number [digits 'A' thru 'E' in uppercase]
  987.    f   floating point number (digits, decimal point, fraction digits)
  988.    e   exponential (scientific notation) number (digit, decimal
  989.          point, fraction digits, letter 'e', sign '+' or '-',
  990.          exponent digits)
  991.    g   'fractional' number in either 'e' or 'f' format, whichever
  992.          produces shorter result
  993.  
  994.  Several optional modifiers can be placed between the initiating
  995.  percent sign and the format character (doesn't apply to %%).
  996.    -   left justify (only matters when width specifier is present)
  997.      (space) for numeric specifiers, prefix nonnegative values with
  998.          a space and negative values with a minus sign
  999.    +   for numeric specifiers, prefix nonnegative values with a plus
  1000.          sign and negative values with a minus sign
  1001.    #   alternate form applicable to several of the format characters
  1002.        (o, x, X, e, E, f, g, G)
  1003.    NN  width ['NN' represents 1 or more decimal digits]; actually
  1004.          minimum width to use, longer items will not be truncated; a
  1005.          leading 0 will cause right-justified numbers to be padded on
  1006.          the left with zeroes instead of spaces when they're aligned
  1007.    .MM precision [decimal point followed by 1 or more digits]; used
  1008.          as maximum width for strings (causing truncation if they're
  1009.          actually longer) or as number of fraction digits for 'f' or
  1010.          'e' numeric formats, or number of significant digits for 'g'
  1011.          numeric format
  1012. 4 user_defined_functions
  1013.  User-defined functions may be created as needed to simplify awk
  1014.  programs or to collect commonly used code into one place.  The
  1015.  general syntax of a user-defined function is the 'function' keyword
  1016.  followed by unique function name, followed by a comma-separated
  1017.  parameter list enclosed in parentheses, followed by statement(s)
  1018.  enclosed within braces ({}).  A 'return' statement is customary
  1019.  but is not required.
  1020.        function FuncName(arg1,arg2) {
  1021.            # arbitrary statements
  1022.            return (arg1 + arg2) / 2
  1023.        }
  1024.  If a function does not use 'return' to specify an output value, the
  1025.  result received by the caller will be unpredictable.
  1026.  
  1027.  Functions may be placed in an awk program before, between, or after
  1028.  the pattern-action rules.  The abbreviation 'func' may be used in
  1029.  place of 'function', unless POSIX compatibility mode is in effect.
  1030. 3 regular_expressions
  1031.  A regular expression is a shorthand way of specifying a 'wildcard'
  1032.  type of string comparison.  Regular expression matching is very
  1033.  fundamental to awk's operation.
  1034.  
  1035.  Meta symbols
  1036.    ^   matches beginning of line or beginning of string; note that
  1037.          embedded newlines ('\n') create multi-line strings, so
  1038.          beginning of line is not necessarily beginning of string
  1039.    $   matches end of line or end of string
  1040.    .   any single character (except newline)
  1041.    [ ] set of characters; [ABC] matches either 'A' or 'B' or 'C'; a
  1042.          dash (other than first or last of the set) denotes a range
  1043.          of characters: [A-Z] matches any upper case letter; if the
  1044.          first character of the set is '^', then the sense of match
  1045.          is reversed: [^0-9] matches any non-digit; several
  1046.          characters need to be quoted with backslash (\) if they
  1047.          occur in a set:  '\', ']', '-', and '^'; within sets,
  1048.          various special character class designations are recognized,
  1049.          such as [:digit:] and [:punct:], as per POSIX
  1050.    |   alternation (similar to boolean 'or'); match either of two
  1051.          patterns [for example "^start|stop$" matches leading 'start'
  1052.          or trailing 'stop']
  1053.    ( ) grouping, alter normal precedence [for example, "^(start|stop)$"
  1054.          matches lines reading either 'start' or 'stop']
  1055.    *   repeated matching; when placed after a pattern, indicates that
  1056.          the pattern should match any number of times [for example,
  1057.          "[a-z][0-9]*" matches a lower case letter followed by zero or
  1058.          more digits]
  1059.    +   repeated matching; when placed after a pattern, indicates that
  1060.          the pattern should match one or more times ["[0-9]+" matches
  1061.          any non-empty sequence of digits]
  1062.    ?   optional matching; indicates that the pattern can match zero or
  1063.          one times ["[a-z][0-9]?" matches lower case letter alone or
  1064.          followed by a single digit]
  1065.    { } interval specification; {n} to match n times or {m,n} to match
  1066.          at least m but not more than n times; only functional when
  1067.          either the `-W posix' or `-W re-interval' options are used
  1068.    \   quote; prevent the character which follows from having special
  1069.          meaning; if the regexp is specified as a string, then the
  1070.          backslash itself will need to be quoted by preceding it with
  1071.          another backslash
  1072.  
  1073.  A regular expression which matches a string or line will match against
  1074.  the first (left-most) substring which meets the pattern and include
  1075.  the longest sequence of characters which still meets that pattern.
  1076. 3 comments
  1077.  Comments in awk programs are introduced with '#'.  Anything after
  1078.  '#' on a line is ignored by GAWK.  It's a good idea to include an
  1079.  explanation of what an awk program is doing and also who wrote it
  1080.  and when.
  1081. 3 further_information
  1082.  For complete documentation on GAWK, see "Effective AWK Programming"
  1083.  by Arnold Robbins.  The second edition (ISBN 1-57831-000-8) is jointly
  1084.  published by SSC and the FSF (http://www.ssc.com).
  1085.  
  1086.  Source text for it is present in the file GAWK.TEXI.  A postscript
  1087.  version is available via anonymous FTP from host prep.ai.mit.edu in
  1088.  directory pub/gnu/, file gawk-{version}-doc.tar.gz where {version}
  1089.  would be the current version number, such as 3.0.3.
  1090.  
  1091.  Another source of documentation is "The AWK Programming Language"
  1092.  by Aho, Weinberger, and Kernighan (1988), published by Addison-Wesley.
  1093.  ISBN code is 0-201-07981-X.
  1094.  
  1095.  Each of these works contains both a reference on the awk language
  1096.  and a tutorial on awk's use, with many sample programs.
  1097. 3 authors
  1098.  The awk programming language was originally created by Alfred V. Aho,
  1099.  Peter J. Weinberger, and Brian W. Kernighan in 1977.  The language
  1100.  was revised and enhanced in a new version which was released in 1985.
  1101.  
  1102.  GAWK, the GNU implementation of awk, was written in 1986 by Paul Rubin
  1103.  and Jay Fenlason, with advice from Richard Stallman, and with
  1104.  contributions from John Woods.  In 1988 and 1989, David Trueman and
  1105.  Arnold Robbins revised GAWK for compatibility with the newer awk.
  1106.  Arnold Robbins is the current maintainer.
  1107.  
  1108.  GAWK version 2.11.1 was ported to VMS by Pat Rankin in November, 1989,
  1109.  with further revisions in the Spring of 1990.  The VMS port was
  1110.  incorporated into the official GNU distribution of version 2.13 in
  1111.  Spring 1991.  (Version 2.12 was never publically released.)
  1112. 2 release_notes
  1113.  GAWK 3.0.3 tested under VAX/VMS V6.2 and Alpha/VMS V6.2, April, 1997;
  1114.  should be compatible with VMS versions V4.6 and later.  Current source
  1115.  code is compatible with DEC's DEC C v5.x or VAX C v3.2; also compiles
  1116.  successfully with GNU C (tested with gcc-vms 2.7.1).
  1117. 3 AWK_LIBRARY
  1118.  GAWK uses a built in search path when looking for a program file
  1119.  specified by the -f option (or the /input qualifier) when that file
  1120.  name does not include a device and/or directory.  GAWK will first
  1121.  look in the current default directory, then if the file wasn't found
  1122.  it will look in the directory specified by the translation of logical
  1123.  name "AWK_LIBRARY".
  1124.  
  1125.  Not applicable under VMS POSIX.
  1126. 3 known_problems
  1127.  There are several known problems with GAWK running on VMS.  Some can
  1128.  be ignored, others require work-arounds.  Note: GAWK in the VMS POSIX
  1129.  environment does not have these problems.
  1130. 4 command_line_parsing
  1131.  The command
  1132.        gawk "program text"
  1133.  will pass the first phase of DCL parsing (the single required
  1134.  parameter is present), then it will give an error that a required
  1135.  element (either /input=awk_file or /commands="program text") is
  1136.  missing.  If what was intended (as is most likely) is to pass the
  1137.  program text to the UN*X-style command interface, the following
  1138.  variation is required
  1139.        gawk -- "program text"
  1140.  The presence of "--", which is normally optional, will inhibit the
  1141.  attempt to use DCL parsing (as will any '-' option or redirection).
  1142. 4 file_formats
  1143.  If a file having the RMS attribute "Fortran carriage control" is
  1144.  read as input, it will generate an empty first record if the first
  1145.  actual record begins with a space (leading space becomes a newline).
  1146.  Also, the last record of the file will give a "record not terminated"
  1147.  warning.  Both of these minor problems are due to the way that the
  1148.  C Run-Time Library (VAXCRTL) converts record attributes.
  1149.  
  1150.  Another poor feature without a work-around is that there's no way to
  1151.  specify "append if possible, create with RMS text attributes if not"
  1152.  with the current command line I/O redirection.  '>>$' isn't supported.
  1153.  Ditto for binary output; '>>+' isn't supported.
  1154. 4 RS_peculiarities
  1155.  Changing the record separator to something other than newline ('\n')
  1156.  will produce anomalous results for ordinary files.  For example,
  1157.  using RS = "\f" and FS = "\n" with the following input
  1158.        |rec 1, line 1
  1159.        |rec 1, line 2
  1160.        |^L    (form feed)
  1161.        |rec 2, line 1
  1162.        |rec 2, line 2
  1163.        |^L    (form feed)
  1164.        |rec 3, line 1
  1165.        |rec 3, line 2
  1166.        |(end of file)
  1167.  will produce two fields for record 1, but three fields each for
  1168.  records 2 and 3.  This is because the form-feed record delimiter is
  1169.  on its own line, so awk sees a newline after it.  Since newline is
  1170.  now a field separator, records 2 and 3 will have null first fields.
  1171.  The following awk code will work-around this problem by inserting
  1172.  a null first field in the first record, so that all records can be
  1173.  handled the same by subsequent processing.
  1174.        # fix up for first record (RS != "\n")
  1175.        FNR == 1  { if ( $0 == "" )     #leading separator
  1176.                      next              #skip its null record
  1177.                    else                #otherwise,
  1178.                      $0 = FS $0        #realign fields
  1179.                  }
  1180.  There is a second problem with this same example.  It will always
  1181.  trigger a "record not terminated" warning when it reaches the end of
  1182.  file.  In the sample shown, there is no final separator; however, if
  1183.  a trailing form-feed were present, it would produce a spurious final
  1184.  record with two null fields.  This occurs because the I/O system
  1185.  sees an implicit newline at the end of the last record, so awk sees
  1186.  a pair of null fields separated by that newline.  The following code
  1187.  fragment will fix that provided there are no null records (in this
  1188.  case, that would be two consecutive lines containing just form-feeds).
  1189.        # fix up for last record (RS != "\n")
  1190.        $0 == FS  { next }      #drop spurious final record
  1191.  Note that the "record not terminated" warning will persist.
  1192. 4 cmd_inconsistency
  1193.  The DCL qualifier /OUTPUT is internally equivalent to '>$' output
  1194.  redirection, but the qualifier /INPUT corresponds to the -f option
  1195.  rather than to '<' input redirection.
  1196. 4 exit
  1197.  The exit statement can optionally pass a final status value to the
  1198.  operating system.  GAWK expects a UN*X-style value instead of a
  1199.  VMS status value, so 0 indicates success and non-zero indicates
  1200.  failure.  The final exit status will be 1 (VMS success) if 0 is
  1201.  used, or even (VMS non-success) if non-zero is used.
  1202. 3 changes
  1203.  Changes between version 3.0.3 and 2.15.6
  1204.  
  1205.    General
  1206.      RS can contain multiple characters or be a regexp
  1207.      Regular expression interval support added
  1208.      gensub() and fflush() functions added
  1209.      memory leak(s) introduced in 3.0.2 or 3.0.1 fixed
  1210.      the user manual has been substantially revised
  1211.  
  1212.    VMS-specific
  1213.      Switched to build with DEC C by default
  1214.  
  1215. 3 prior_changes
  1216.  Changes between version 2.15.6 and 2.14
  1217.  
  1218.    General
  1219.      Many obscure bugs fixed
  1220.      `delete' may operate on an entire array
  1221.      ARGIND and ERRNO builtin variables added
  1222.  
  1223.    VMS-specific
  1224.      `>+ file' binary-mode output redirection added
  1225.      /variable=(foo=42) fixed
  1226.      Floating point number formatting improved
  1227.  
  1228.  Changes between version 2.14 and 2.13.2:
  1229.  
  1230.    General
  1231.      'next file' construct added
  1232.      'continue' outside of any loop is treated as 'next'
  1233.      Assorted bug fixes and efficiency improvements
  1234.      _The_GAWK_Manual_ updated
  1235.      Test suite expanded
  1236.  
  1237.    VMS-specific
  1238.      VMS POSIX support added
  1239.      Disk I/O throughput enhanced
  1240.      Pipe emulation improved and incorrect interaction with user-mode
  1241.          redefinition of SYS$OUTPUT eliminated
  1242.  
  1243.  Changes between version 2.13 and 2.11.1:  (2.12 was not released)
  1244.  
  1245.    General
  1246.      CONVFMT and FIELDWIDTHS builtin control variables added
  1247.      systime() and strftime() date/time functions added
  1248.      'lint' and 'posix' run-time options added
  1249.      '-W' command line option syntax supercedes '-c', '-C', and '-V'
  1250.      '-a' and '-e' regular expression options made obsolete
  1251.      Various bug fixes and efficiency improvements
  1252.      More platforms supported ('officially' including VMS)
  1253.  
  1254.    VMS-specific
  1255.      %g printf format fixed
  1256.      Handling of '\' on command line modified; no longer necessary to
  1257.          double it up
  1258.      Problem redirecting stderr (>&efile) at same time as stdin (<ifile)
  1259.          or stdout (>ofile) has been fixed
  1260.      ``2>&1'' and ``1>&2'' redirection constructs added
  1261.      Interaction between command line I/O redirection and gawk pipes
  1262.          fixed; also, name used for pseudo-pipe temporary file expanded
  1263. 3 license
  1264.  GAWK is covered by the "GNU General Public License", the gist of which
  1265.  is that if you supply this software to a third party, you are expressly
  1266.  forbidden to prevent them from supplying it to a fourth party, and if
  1267.  you supply binaries you must make the source code available to them
  1268.  at no additional cost.  Any revisions or modified versions are also
  1269.  covered by the same license.  There is no warranty, express or implied,
  1270.  for this software.  It is provided "as is."
  1271.  
  1272.  [Disclaimer:  This is just an informal summary with no legal basis;
  1273.  refer to the actual GNU General Public License for specific details.]
  1274. !2 examples
  1275. !
  1276.