home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat1 / awk.z / awk
Encoding:
Text File  |  2002-10-03  |  21.1 KB  |  462 lines

  1.  
  2.  
  3.  
  4. aaaawwwwkkkk((((1111))))                                                                  aaaawwwwkkkk((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      aaaawwwwkkkk, nnnnaaaawwwwkkkk, ppppaaaawwwwkkkk  - pattern scanning and processing language
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.      aaaawwwwkkkk [----FFFF _r_e] [----vvvv _v_a_r=_v_a_l_u_e] [''''_p_r_o_g''''] [_f_i_l_e. . .]
  13.      aaaawwwwkkkk [----FFFF _r_e] [----vvvv _v_a_r=_v_a_l_u_e] [----ffff _p_r_o_g_f_i_l_e] [_f_i_l_e. . .]
  14.  
  15. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  16.      NNNNOOOOTTTTEEEE:::: This version of awk has some incompatibilities with previous
  17.      versions. See the COMPATIBILITY ISSUES section below for more detail.
  18.  
  19.      aaaawwwwkkkk and nnnnaaaawwwwkkkk use the old rrrreeeeggggeeeexxxxpppp(((()))) and ccccoooommmmppppiiiilllleeee(((()))) regular expression
  20.      routines.  When the environment variable ____XXXXPPPPGGGG is equal to 1111 (one), ppppaaaawwwwkkkk
  21.      is exec'ed which uses the newer rrrreeeeggggccccoooommmmpppp(((()))) and rrrreeeeggggeeeexxxxeeeecccc(((()))) routines which
  22.      implement the Extended Regular Expression package.
  23.  
  24.      aaaawwwwkkkk scans each input _f_i_l_e for lines that match any of a set of patterns
  25.      specified in _p_r_o_g.  The _p_r_o_g string must be enclosed in single quotes ('''')
  26.      to protect it from the shell.  Patterns are arbitrary Boolean
  27.      combinations of regular expressions and relational expressions.  For each
  28.      pattern in _p_r_o_g there may be an associated action performed when a line
  29.      of a _f_i_l_e matches the pattern.  The set of pattern-action statements may
  30.      appear literally as _p_r_o_g or in a file specified with the ----ffff _p_r_o_g_f_i_l_e
  31.      option.  Input files are read in order; if there are no files, the
  32.      standard input is read.  The file name ---- means the standard input.
  33.  
  34.      aaaawwwwkkkk processes supplementary code set characters in pattern-action
  35.      statements and comments, and recognizes supplementary code set characters
  36.      as field separators (see below) according to the locale specified in the
  37.      LLLLCCCC____CCCCTTTTYYYYPPPPEEEE environment variable [see LLLLAAAANNNNGGGG on eeeennnnvvvviiiirrrroooonnnn(5)].  In regular
  38.      expressions, pattern searches are performed on characters, not bytes, as
  39.      described on eeeedddd(1).
  40.  
  41.      Each input line is matched against the pattern portion of every pattern-
  42.      action statement; the associated action is performed for each matched
  43.      pattern.  Any _f_i_l_e of the form vvvvaaaarrrr=_v_a_l_u_e is treated as an assignment, not
  44.      a filename.  If the environment variable ____XXXXPPPPGGGG is equal to 1111 (one), each
  45.      such assignment is executed at the time it would have been opened if it
  46.      were a filename. If the environment variable ____XXXXPPPPGGGG is not set, all
  47.      assignments that appear before the first actual file are processed as if
  48.      each was preceded by the ----vvvv opton. The option ----vvvv followed by vvvvaaaarrrr=_v_a_l_u_e is
  49.      an assignment to be done before _p_r_o_g is executed; any number of ----vvvv
  50.      options may be present.
  51.  
  52.      An input line is normally made up of fields separated by white space.
  53.      (This default can be changed by using the FFFFSSSS built-in variable or the ----FFFF
  54.      _r_e option.)  The fields are denoted $$$$1111, $$$$2222, ...; $$$$0000 refers to the entire
  55.      line.
  56.  
  57.      A pattern-action statement has the form:
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. aaaawwwwkkkk((((1111))))                                                                  aaaawwwwkkkk((((1111))))
  71.  
  72.  
  73.  
  74.           _p_a_t_t_e_r_n {{{{ _a_c_t_i_o_n }}}}
  75.  
  76.      Either pattern or action may be omitted.  If there is no action with a
  77.      pattern, the matching line is printed.  If there is no pattern with an
  78.      action, the action is performed on every input line.  Pattern-action
  79.      statements are separated by newlines or semicolons.
  80.  
  81.      As noted, patterns are arbitrary Boolean combinations ( !!!!, ||, &&&&&&&&, and
  82.      parentheses) of relational expressions and regular expressions.  A
  83.      relational expression is one of the following:
  84.  
  85.           _e_x_p_r_e_s_s_i_o_n _r_e_l_o_p _e_x_p_r_e_s_s_i_o_n
  86.           _e_x_p_r_e_s_s_i_o_n _m_a_t_c_h_o_p _r_e_g_u_l_a_r__e_x_p_r_e_s_s_i_o_n
  87.           _e_x_p_r_e_s_s_i_o_n iiiinnnn _a_r_r_a_y-_n_a_m_e
  88.           ((((_e_x_p_r_e_s_s_i_o_n,,,,_e_x_p_r_e_s_s_i_o_n,,,, ...  )))) iiiinnnn _a_r_r_a_y-_n_a_m_e
  89.  
  90.      where a _r_e_l_o_p is any of the six relational operators in C, and a _m_a_t_c_h_o_p
  91.      is either ~~~~ (contains) or !!!!~~~~ (does not contain).  An _e_x_p_r_e_s_s_i_o_n is an
  92.      arithmetic expression, a relational expression, the special expression
  93.  
  94.           _v_a_r iiiinnnn _a_r_r_a_y
  95.  
  96.      or a Boolean combination of these.
  97.  
  98.      Regular expressions are as in eeeeggggrrrreeeepppp(1).  In patterns they must be
  99.      surrounded by slashes.  Isolated regular expressions in a pattern apply
  100.      to the entire line.  Regular expressions may also occur in relational
  101.      expressions.  A pattern may consist of two patterns separated by a comma;
  102.      in this case, the action is performed for all lines between an occurrence
  103.      of the first pattern and the next occurrence of the second pattern.
  104.  
  105.      The special patterns BBBBEEEEGGGGIIIINNNN and EEEENNNNDDDD may be used to capture control before
  106.      the first input line has been read and after the last input line has been
  107.      read respectively.  These keywords do not combine with any other
  108.      patterns.
  109.  
  110.      A regular expression may be used to separate fields by using the ----FFFF _r_e
  111.      option or by assigning the expression to the built-in variable FS.  The
  112.      default is to ignore leading blanks and to separate fields by blanks
  113.      and/or tab characters.  However, if FS is assigned a value, leading
  114.      blanks are no longer ignored.
  115.  
  116.      Other built-in variables include:
  117.  
  118.           AAAARRRRGGGGCCCC            command line argument count
  119.  
  120.           AAAARRRRGGGGVVVV            command line argument array
  121.  
  122.           EEEENNNNVVVVIIIIRRRROOOONNNN         array of environment variables; subscripts are names
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. aaaawwwwkkkk((((1111))))                                                                  aaaawwwwkkkk((((1111))))
  137.  
  138.  
  139.  
  140.           FFFFIIIILLLLEEEENNNNAAAAMMMMEEEE        name of the current input file
  141.  
  142.           FFFFNNNNRRRR             ordinal number of the current record in the current
  143.                           file
  144.  
  145.           FFFFSSSS              input field separator regular expression (default
  146.                           blank and tab)
  147.  
  148.           NNNNFFFF              number of fields in the current record
  149.  
  150.           NNNNRRRR              ordinal number of the current record
  151.  
  152.           OOOOFFFFMMMMTTTT            output format for numbers (default %%%%....6666gggg)
  153.  
  154.           OOOOFFFFSSSS             output field separator (default blank)
  155.  
  156.           OOOORRRRSSSS             output record separator (default new-line)
  157.  
  158.           RRRRSSSS              input record separator (default new-line)
  159.  
  160.           SSSSUUUUBBBBSSSSEEEEPPPP          separates multiple subscripts (default is 034)
  161.  
  162.      The field separators specified with the ----FFFF option or with the variables
  163.      OOOOFFFFSSSS, OOOORRRRSSSS, and FFFFSSSS may be supplementary code set characters.
  164.  
  165.      An action is a sequence of statements.  A statement may be one of the
  166.      following:
  167.  
  168.           iiiiffff ( _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t [ eeeellllsssseeee _s_t_a_t_e_m_e_n_t ]
  169.           wwwwhhhhiiiilllleeee ( _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t
  170.           ddddoooo _s_t_a_t_e_m_e_n_t wwwwhhhhiiiilllleeee ( _e_x_p_r_e_s_s_i_o_n )
  171.           ffffoooorrrr ( _e_x_p_r_e_s_s_i_o_n ; _e_x_p_r_e_s_s_i_o_n ; _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t
  172.           ffffoooorrrr ( _v_a_r iiiinnnn _a_r_r_a_y ) _s_t_a_t_e_m_e_n_t
  173.           ddddeeeelllleeeetttteeee _a_r_r_a_y[_s_u_b_s_c_r_i_p_t] #delete an array element
  174.           bbbbrrrreeeeaaaakkkk
  175.           ccccoooonnnnttttiiiinnnnuuuueeee
  176.           { [ _s_t_a_t_e_m_e_n_t ] ... }
  177.           _e_x_p_r_e_s_s_i_o_n     # commonly variable = expression
  178.           pppprrrriiiinnnntttt [ _e_x_p_r_e_s_s_i_o_n-_l_i_s_t ] [ >_e_x_p_r_e_s_s_i_o_n ]
  179.           pppprrrriiiinnnnttttffff _f_o_r_m_a_t [ , _e_x_p_r_e_s_s_i_o_n-_l_i_s_t ] [ >_e_x_p_r_e_s_s_i_o_n ]
  180.           nnnneeeexxxxtttt      # skip remaining patterns on this input line
  181.           eeeexxxxiiiitttt [eeeexxxxpppprrrr]    # skip the rest of the input; exit status is expr
  182.           rrrreeeettttuuuurrrrnnnn [eeeexxxxpppprrrr]
  183.  
  184.      Statements are terminated by semicolons, new-lines, or right braces.  An
  185.      empty expression-list stands for the whole input line.  Expressions take
  186.      on string or numeric values as appropriate, and are built using the
  187.      operators ++++, ----, ****, ////, %%%%, ^^^^ and concatenation (indicated by a blank).  The
  188.      operators ++++++++ -------- ++++==== ----==== ****==== ////==== %%%%==== ^^^^==== >>>> >>>>==== <<<< <<<<==== ======== !!!!==== ????::::  are also available
  189.      in expressions.  Variables may be scalars, array elements (denoted x[i]),
  190.      or fields.  Variables are initialized to the null string or zero.  Array
  191.      subscripts may be any string, not necessarily numeric; this allows for a
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. aaaawwwwkkkk((((1111))))                                                                  aaaawwwwkkkk((((1111))))
  203.  
  204.  
  205.  
  206.      form of associative memory.  Multiple subscripts such as [[[[iiii,,,,jjjj,,,,kkkk]]]] are
  207.      permitted; the constituents are concatenated, separated by the value of
  208.      SSSSUUUUBBBBSSSSEEEEPPPP.  String constants are quoted (""""""""), with the usual C escapes
  209.      recognized within.
  210.  
  211.      A comment consists of any characters beginning with the number sign
  212.      character and terminated by, but excluding the next occurrence of, a
  213.      newline character. Comments will have no effect, except to delimit
  214.      statements.
  215.  
  216.      The pppprrrriiiinnnntttt statement prints its arguments on the standard output, or on a
  217.      file if >>>>_e_x_p_r_e_s_s_i_o_n is present, or on a pipe if | _c_m_d is present.  The
  218.      arguments are separated by the current output field separator and
  219.      terminated by the output record separator.  The pppprrrriiiinnnnttttffff statement formats
  220.      its expression list according to the format (see pppprrrriiiinnnnttttffff(3S)).  The
  221.      built-in function cccclllloooosssseeee(_e_x_p_r) closes the file or pipe _e_x_p_r.
  222.  
  223.      The mathematical functions:  aaaattttaaaannnn2222, ccccoooossss, eeeexxxxpppp, lllloooogggg, ssssiiiinnnn, ssssqqqqrrrrtttt, are built-
  224.      in.
  225.  
  226.      Other built-in functions include:
  227.  
  228.      ggggssssuuuubbbb(_f_o_r, _r_e_p_l, _i_n)
  229.                behaves like ssssuuuubbbb (see below), except that it replaces
  230.                successive occurrences of the regular expression (like the eeeedddd
  231.                global substitute command).
  232.  
  233.      iiiinnnnddddeeeexxxx((((_s,,,, _t))))
  234.                returns the position in string _s where string _t first occurs,
  235.                or 0 if it does not occur at all.
  236.  
  237.      iiiinnnntttt       truncates to an integer value.
  238.  
  239.      lllleeeennnnggggtttthhhh((((_s)))) returns the length in bytes of its argument taken as a string,
  240.                or of the whole line if there is no argument.
  241.  
  242.      mmmmaaaattttcccchhhh((((_s,,,, _r_e))))
  243.                returns the position in string _s where the regular expression
  244.                _r_e occurs, or 0 if it does not occur at all.  RRRRSSSSTTTTAAAARRRRTTTT is set to
  245.                the starting position (which is the same as the returned
  246.                value), and RRRRLLLLEEEENNNNGGGGTTTTHHHH is set to the length of the matched string.
  247.  
  248.      rrrraaaannnndddd      random number on (0, 1).
  249.  
  250.      sssspppplllliiiitttt((((_s,,,, _a,,,, _f_s))))
  251.                splits the string _s into array elements _a[_1], _a[_2], _a[_n], and
  252.                returns _n.  The separation is done with the regular expression
  253.                _f_s or with the field separator FFFFSSSS if _f_s is not given.
  254.  
  255.      ssssrrrraaaannnndddd     sets the seed for rrrraaaannnndddd
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. aaaawwwwkkkk((((1111))))                                                                  aaaawwwwkkkk((((1111))))
  269.  
  270.  
  271.  
  272.      sssspppprrrriiiinnnnttttffff((((_f_m_t,,,, _e_x_p_r,,,, _e_x_p_r,,,,...))))
  273.                formats the expressions according to the pppprrrriiiinnnnttttffff(3S) format
  274.                given by _f_m_t and returns the resulting string.
  275.  
  276.      ssssuuuubbbb(_f_o_r, _r_e_p_l, _i_n)
  277.                substitutes the string _r_e_p_l in place of the first instance of
  278.                the regular expression _f_o_r in string _i_n and returns the number
  279.                of substitutions.  If _i_n is omitted, aaaawwwwkkkk substitutes in the
  280.                current record ($$$$0000).
  281.  
  282.      ssssuuuubbbbssssttttrrrr(_s, _m, _n)
  283.                returns the _n-byte substring of _s that begins at position _m.
  284.  
  285.      ttttoooolllloooowwwweeeerrrr(_s)
  286.                converts all upper-case alphabetic characters in string _s to
  287.                lower-case.  Numbers and other characters are not affected.
  288.  
  289.      ttttoooouuuuppppppppeeeerrrr(_s)
  290.                converts all lower-case alphabetic characters in string _s to
  291.                upper-case. Numbers and other characters are not affected.
  292.  
  293.  
  294.      The input/output built-in functions are:
  295.  
  296.      cccclllloooosssseeee((((_f_i_l_e_n_a_m_e))))
  297.                closes the file or pipe named _f_i_l_e_n_a_m_e.
  298.  
  299.      _c_m_d |||| ggggeeeettttlllliiiinnnneeee
  300.                pipes the output of _c_m_d into ggggeeeettttlllliiiinnnneeee; each successive call to
  301.                _g_e_t_l_i_n_e returns the next line of output from _c_m_d.
  302.  
  303.      ggggeeeettttlllliiiinnnneeee   sets $$$$0000 to the next input record from the current input file.
  304.  
  305.      ggggeeeettttlllliiiinnnneeee <<<<_f_i_l_e
  306.                sets $$$$0000 to the next record from _f_i_l_e.
  307.  
  308.      ggggeeeettttlllliiiinnnneeee _x sets variable _x instead.
  309.  
  310.      ggggeeeettttlllliiiinnnneeee _x <<<<_f_i_l_e
  311.                sets _x from the next record of _f_i_l_e.
  312.  
  313.      ssssyyyysssstttteeeemmmm((((_c_m_d))))
  314.                executes _c_m_d and returns its exit status.
  315.  
  316.      All forms of ggggeeeettttlllliiiinnnneeee return 1 for successful input, 0 for end of file,
  317.      and -1 for an error.
  318.  
  319.      aaaawwwwkkkk also provides user-defined functions.  Such functions may be defined
  320.      (in the pattern position of a pattern-action statement) as
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. aaaawwwwkkkk((((1111))))                                                                  aaaawwwwkkkk((((1111))))
  335.  
  336.  
  337.  
  338.           _ffff_uuuu_nnnn_cccc_tttt_iiii_oooo_nnnn _n_a_m_e_((((_a_r_g_s_,,,,..._)))) _{{{{ _s_t_m_t_s _}}}}
  339.  
  340.      Function arguments are passed by value if scalar and by reference if
  341.      array name.  Argument names are local to the function; all other variable
  342.      names are global.  Function calls may be nested and functions may be
  343.      recursive.  The rrrreeeettttuuuurrrrnnnn statement may be used to return a value.
  344.  
  345. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  346.      Print lines longer than 72 characters:
  347.  
  348.           length > 72
  349.  
  350.      Print first two fields in opposite order:
  351.  
  352.           { print $2, $1 }
  353.  
  354.      Same, with input fields separated by comma and/or blanks and tabs:
  355.      BEGIN { FS = ",[ \t]*|[ \t]+" }
  356.      { print $2, $1 }
  357.  
  358.  
  359.      Add up first column, print sum and average:
  360.  
  361.           { s += $1 }
  362.           END { print "sum is", s, " average is", s/NR }
  363.  
  364.      Print fields in reverse order:
  365.  
  366.           { for (i = NF; i > 0; --i) print $i }
  367.  
  368.      Print all lines between start/stop pairs:
  369.  
  370.           /start/, /stop/
  371.  
  372.      Print all lines whose first field is different from previous one:
  373.  
  374.           $1 != prev { print; prev = $1 }
  375.  
  376.      Simulate eeeecccchhhhoooo(1):
  377.  
  378.           BEGIN {
  379.                for (i = 1; i < ARGC; i++)
  380.                     printf "%s", ARGV[i]
  381.                printf "\n"
  382.                exit
  383.           }
  384.  
  385.      Print a file, filling in page numbers starting at 5:
  386.  
  387.  
  388.  
  389.  
  390.  
  391.                                                                         PPPPaaaaggggeeee 6666
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398. aaaawwwwkkkk((((1111))))                                                                  aaaawwwwkkkk((((1111))))
  399.  
  400.  
  401.  
  402.           /Page/ { $2 = n++; }
  403.           { print }
  404.  
  405.      Assuming this program is in a file named pppprrrroooogggg, the following command line
  406.      prints the file iiiinnnnppppuuuutttt numbering its pages starting at 5:  awk -f prog n=5
  407.      input.
  408.  
  409. FFFFIIIILLLLEEEESSSS
  410.      /usr/lib/locale/locale/LC_MESSAGES/uxawk
  411.           language-specific message file (see LLLLAAAANNNNGGGG on eeeennnnvvvviiiirrrroooonnnn(5))
  412.  
  413. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  414.      ooooaaaawwwwkkkk(1), eeeeggggrrrreeeepppp(1), ggggrrrreeeepppp(1), lllleeeexxxx(1), ppppeeeerrrrllll(1), sssseeeedddd(1), pppprrrriiiinnnnttttffff(3S)
  415.      A. V. Aho, B. W. Kernighan, P. J. Weinberger, _T_h_e _a_w_k _P_r_o_g_r_a_m_m_i_n_g
  416.      _L_a_n_g_u_a_g_e Addison-Wesley, 1988
  417.  
  418. NNNNOOOOTTTTEEEESSSS aaaannnndddd CCCCOOOOMMMMPPPPAAAATTTTIIIIBBBBIIIILLLLIIIITTTTYYYY IIIISSSSSSSSUUUUEEEESSSS
  419.      aaaawwwwkkkk is a newer version that provides capabilities unavailable in previous
  420.      versions.  See ooooaaaawwwwkkkk(1) for the older version.
  421.  
  422.      Input white space is not preserved on output if fields are involved.
  423.  
  424.      There are no explicit conversions between numbers and strings.  To force
  425.      an expression to be treated as a number add 0 to it; to force it to be
  426.      treated as a string concatenate the null string ("""""""") to it.
  427.  
  428.      The following regular expressions are no longer accepted:
  429.  
  430.          ////[[[[]]]]////     ////[[[[^^^^]]]]////      ////[[[[\\\\]]]]]]]]////
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.                                                                         PPPPaaaaggggeeee 7777
  458.  
  459.  
  460.  
  461.