home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR7 / GAWK215.ZIP / GAWK.MAN < prev    next >
Text File  |  1993-07-24  |  59KB  |  1,387 lines

  1.  
  2.  
  3.  
  4. GAWK(1)                  Utility Commands                 GAWK(1)
  5.  
  6.  
  7. NNAAMMEE
  8.        gawk - pattern scanning and processing language
  9.  
  10. SSYYNNOOPPSSIISS
  11.        ggaawwkk [ POSIX or GNU style options ] --ff _p_r_o_g_r_a_m_-_f_i_l_e [ ---- ]
  12.        file ...
  13.        ggaawwkk [ POSIX or GNU style options ] [  ----  ]  _p_r_o_g_r_a_m_-_t_e_x_t
  14.        file ...
  15.  
  16. DDEESSCCRRIIPPTTIIOONN
  17.        _G_a_w_k  is  the GNU Project's implementation of the AWK pro-
  18.        gramming language.  It conforms to the definition  of  the
  19.        language  in  the POSIX 1003.2 Command Language And Utili-
  20.        ties Standard.  This version  in  turn  is  based  on  the
  21.        description  in  _T_h_e  _A_W_K  _P_r_o_g_r_a_m_m_i_n_g  _L_a_n_g_u_a_g_e,  by Aho,
  22.        Kernighan, and Weinberger, with  the  additional  features
  23.        defined  in  the  System  V Release 4 version of UNIX _a_w_k.
  24.        _G_a_w_k also provides some GNU-specific extensions.
  25.  
  26.        The command line consists of options to _g_a_w_k  itself,  the
  27.        AWK  program  text  (if  not supplied via the --ff or ----ffiillee
  28.        options), and values to be made available in the AARRGGCC  and
  29.        AARRGGVV pre-defined AWK variables.
  30.  
  31. OOPPTTIIOONNSS
  32.        _G_a_w_k  options may be either the traditional POSIX one let-
  33.        ter options, or the GNU style long options.   POSIX  style
  34.        options  start with a single ``-'', while GNU long options
  35.        start with ``--''.  GNU style long  options  are  provided
  36.        for both GNU-specific features and for POSIX mandated fea-
  37.        tures.  Other implementations  of  the  AWK  language  are
  38.        likely  to only accept the traditional one letter options.
  39.  
  40.        Following the POSIX standard,  _g_a_w_k-specific  options  are
  41.        supplied  via  arguments  to  the  --WW option.  Multiple --WW
  42.        options may be supplied, or multiple arguments may be sup-
  43.        plied  together  if  they  are  separated  by  commas,  or
  44.        enclosed in quotes and separated by white space.  Case  is
  45.        ignored in arguments to the --WW option.  Each --WW option has
  46.        a corresponding GNU style long option, as detailed  below.
  47.  
  48.        _G_a_w_k accepts the following options.
  49.  
  50.        --FF _f_s
  51.        ----ffiieelldd--sseeppaarraattoorr==_f_s
  52.               Use  _f_s for the input field separator (the value of
  53.               the FFSS predefined variable).
  54.  
  55.        --vv _v_a_r==_v_a_l
  56.        ----aassssiiggnn==_v_a_r==_v_a_l
  57.               Assign the value _v_a_l, to the variable  _v_a_r,  before
  58.               execution  of  the  program  begins.  Such variable
  59.               values are available to the BBEEGGIINN block of  an  AWK
  60.               program.
  61.  
  62.  
  63.  
  64. Free Software Foundation   Apr 15 1993                          1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. GAWK(1)                  Utility Commands                 GAWK(1)
  71.  
  72.  
  73.        --ff _p_r_o_g_r_a_m_-_f_i_l_e
  74.        ----ffiillee==_p_r_o_g_r_a_m_-_f_i_l_e
  75.               Read  the AWK program source from the file _p_r_o_g_r_a_m_-
  76.               _f_i_l_e, instead of from the first command line  argu-
  77.               ment.  Multiple --ff (or ----ffiillee) options may be used.
  78.  
  79.        --WW ccoommppaatt
  80.        ----ccoommppaatt    Run in _c_o_m_p_a_t_i_b_i_l_i_t_y mode.   In  compatibility
  81.                    mode,  _g_a_w_k  behaves  identically to UNIX _a_w_k;
  82.                    none of the GNU-specific extensions are recog-
  83.                    nized.   See  GGNNUU  EEXXTTEENNSSIIOONNSS, below, for more
  84.                    information.
  85.  
  86.        --WW ccooppyylleefftt
  87.        --WW ccooppyyrriigghhtt
  88.        ----ccooppyylleefftt
  89.        ----ccooppyyrriigghhtt Print the short version of the  GNU  copyright
  90.                    information message on the error output.
  91.  
  92.        --WW hheellpp
  93.        --WW uussaaggee
  94.        ----hheellpp
  95.        ----uussaaggee     Print a relatively short summary of the avail-
  96.                    able options on the error output.
  97.  
  98.        --WW lliinntt
  99.        ----lliinntt      Provide warnings  about  constructs  that  are
  100.                    dubious or non-portable to other AWK implemen-
  101.                    tations.
  102.        --WW ppoossiixx
  103.        ----ppoossiixx     This turns on  _c_o_m_p_a_t_i_b_i_l_i_t_y  mode,  with  the
  104.                    following additional restrictions:
  105.  
  106.                    o \\xx escape sequences are not recognized.
  107.  
  108.                    o The synonym ffuunncc for the keyword ffuunnccttiioonn is
  109.                      not recognized.
  110.  
  111.                    o The operators **** and ****== cannot be  used  in
  112.                      place of ^^ and ^^==.
  113.  
  114.        --WW ssoouurrccee==_p_r_o_g_r_a_m_-_t_e_x_t
  115.        ----ssoouurrccee==_p_r_o_g_r_a_m_-_t_e_x_t
  116.                    Use  _p_r_o_g_r_a_m_-_t_e_x_t  as AWK program source code.
  117.                    This option allows  the  easy  intermixing  of
  118.                    library  functions (used via the --ff and ----ffiillee
  119.                    options) with source code entered on the  com-
  120.                    mand  line.   It  is  intended  primarily  for
  121.                    medium to large  size  AWK  programs  used  in
  122.                    shell scripts.
  123.                    The  --WW  ssoouurrccee==  form of this option uses the
  124.                    rest of the command line argument for _p_r_o_g_r_a_m_-
  125.                    _t_e_x_t;  no  other  options to --WW will be recog-
  126.                    nized in the same argument.
  127.  
  128.  
  129.  
  130. Free Software Foundation   Apr 15 1993                          2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. GAWK(1)                  Utility Commands                 GAWK(1)
  137.  
  138.  
  139.        --WW vveerrssiioonn
  140.        ----vveerrssiioonn   Print version information for this  particular
  141.                    copy  of  _g_a_w_k  on  the error output.  This is
  142.                    useful mainly for knowing if the current  copy
  143.                    of  _g_a_w_k  on  your  system  is up to date with
  144.                    respect to whatever the Free Software  Founda-
  145.                    tion is distributing.
  146.  
  147.        ----          Signal  the  end of options. This is useful to
  148.                    allow further arguments  to  the  AWK  program
  149.                    itself  to start with a ``-''.  This is mainly
  150.                    for consistency with the argument parsing con-
  151.                    vention used by most other POSIX programs.
  152.  
  153.        Any  other  options are flagged as illegal, but are other-
  154.        wise ignored.
  155.  
  156. AAWWKK PPRROOGGRRAAMM EEXXEECCUUTTIIOONN
  157.        An AWK program consists of a  sequence  of  pattern-action
  158.        statements and optional function definitions.
  159.  
  160.               _p_a_t_t_e_r_n   {{ _a_c_t_i_o_n _s_t_a_t_e_m_e_n_t_s }}
  161.               ffuunnccttiioonn _n_a_m_e((_p_a_r_a_m_e_t_e_r _l_i_s_t)) {{ _s_t_a_t_e_m_e_n_t_s }}
  162.  
  163.        _G_a_w_k  first  reads  the  program  source from the _p_r_o_g_r_a_m_-
  164.        _f_i_l_e(s) if specified, or from the first  non-option  argu-
  165.        ment  on the command line.  The --ff option may be used mul-
  166.        tiple times on the command line.  _G_a_w_k will read the  pro-
  167.        gram  text  as  if all the _p_r_o_g_r_a_m_-_f_i_l_es had been concate-
  168.        nated together.  This is useful for building libraries  of
  169.        AWK  functions, without having to include them in each new
  170.        AWK program that uses them.  To use a library function  in
  171.        a  file from a program typed in on the command line, spec-
  172.        ify //ddeevv//ttttyy as one of the _p_r_o_g_r_a_m_-_f_i_l_es, type  your  pro-
  173.        gram, and end it with a ^^DD (control-d).
  174.  
  175.        The  environment  variable AAWWKKPPAATTHH specifies a search path
  176.        to use when finding source files named with the --ff option.
  177.        If  this  variable  does  not  exist,  the default path is
  178.        ""..:://uussrr//lliibb//aawwkk:://uussrr//llooccaall//lliibb//aawwkk"".  If a file name given
  179.        to  the  --ff  option  contains  a  ``/'' character, no path
  180.        search is performed.
  181.  
  182.        _G_a_w_k executes AWK programs in the following order.  First,
  183.        _g_a_w_k  compiles  the  program into an internal form.  Next,
  184.        all variable assignments specified via the --vv  option  are
  185.        performed.   Then,  _g_a_w_k  executes  the  code in the BBEEGGIINN
  186.        block(s) (if any), and then proceeds  to  read  each  file
  187.        named  in  the AARRGGVV array.  If there are no files named on
  188.        the command line, _g_a_w_k reads the standard input.
  189.  
  190.        If a filename on the command line has the form _v_a_r==_v_a_l  it
  191.        is treated as a variable assignment. The variable _v_a_r will
  192.        be assigned the value _v_a_l.  (This happens after any  BBEEGGIINN
  193.  
  194.  
  195.  
  196. Free Software Foundation   Apr 15 1993                          3
  197.  
  198.  
  199.  
  200.  
  201.  
  202. GAWK(1)                  Utility Commands                 GAWK(1)
  203.  
  204.  
  205.        block(s) have been run.)  Command line variable assignment
  206.        is most useful for dynamically  assigning  values  to  the
  207.        variables  AWK  uses  to  control how input is broken into
  208.        fields and records. It  is  also  useful  for  controlling
  209.        state  if  multiple  passes  are needed over a single data
  210.        file.
  211.  
  212.        If the value of a particular  element  of  AARRGGVV  is  empty
  213.        (""""), _g_a_w_k skips over it.
  214.  
  215.        For  each  line  in  the  input,  _g_a_w_k  tests to see if it
  216.        matches any _p_a_t_t_e_r_n in the AWK program.  For each  pattern
  217.        that  the line matches, the associated _a_c_t_i_o_n is executed.
  218.        The patterns are tested in the order  they  occur  in  the
  219.        program.
  220.  
  221.        Finally,  after  all the input is exhausted, _g_a_w_k executes
  222.        the code in the EENNDD block(s) (if any).
  223.  
  224. VVAARRIIAABBLLEESS AANNDD FFIIEELLDDSS
  225.        AWK variables are dynamic; they come into  existence  when
  226.        they  are  first  used.  Their values are either floating-
  227.        point numbers or strings, or both, depending upon how they
  228.        are  used.  AWK  also  has  one dimension arrays; multiply
  229.        dimensioned arrays may be simulated.  Several  pre-defined
  230.        variables  are  set  as  a  program  runs;  these  will be
  231.        described as needed and summarized below.
  232.  
  233.    FFiieellddss
  234.        As each input line is read,  _g_a_w_k  splits  the  line  into
  235.        _f_i_e_l_d_s,  using  the  value of the FFSS variable as the field
  236.        separator.  If FFSS is a single character, fields are  sepa-
  237.        rated  by that character.  Otherwise, FFSS is expected to be
  238.        a full regular expression.  In the special case that FFSS is
  239.        a  single  blank,  fields  are separated by runs of blanks
  240.        and/or tabs.  Note  that  the  value  of  IIGGNNOORREECCAASSEE  (see
  241.        below)  will also affect how fields are split when FFSS is a
  242.        regular expression.
  243.  
  244.        If the FFIIEELLDDWWIIDDTTHHSS variable is set to  a  space  separated
  245.        list  of  numbers,  each  field  is expected to have fixed
  246.        width, and _g_a_w_k will split up the record using the  speci-
  247.        fied widths.  The value of FFSS is ignored.  Assigning a new
  248.        value to FFSS overrides the use of FFIIEELLDDWWIIDDTTHHSS, and restores
  249.        the default behavior.
  250.  
  251.        Each  field  in  the  input  line may be referenced by its
  252.        position, $$11, $$22, and so on.  $$00 is the  whole  line.  The
  253.        value  of a field may be assigned to as well.  Fields need
  254.        not be referenced by constants:
  255.  
  256.               nn == 55
  257.               pprriinntt $$nn
  258.  
  259.  
  260.  
  261.  
  262. Free Software Foundation   Apr 15 1993                          4
  263.  
  264.  
  265.  
  266.  
  267.  
  268. GAWK(1)                  Utility Commands                 GAWK(1)
  269.  
  270.  
  271.        prints the fifth field in the input line.  The variable NNFF
  272.        is set to the total number of fields in the input line.
  273.  
  274.        References  to non-existent fields (i.e. fields after $$NNFF)
  275.        produce the null-string.  However,  assigning  to  a  non-
  276.        existent field (e.g., $$((NNFF++22)) == 55) will increase the value
  277.        of NNFF, create any intervening fields with the null  string
  278.        as  their  value,  and  cause the value of $$00 to be recom-
  279.        puted, with the fields being separated  by  the  value  of
  280.        OOFFSS.
  281.  
  282.    BBuuiilltt--iinn VVaarriiaabblleess
  283.        AWK's built-in variables are:
  284.  
  285.  
  286.        AARRGGCC        The number of command line arguments (does not
  287.                    include  options  to  _g_a_w_k,  or  the   program
  288.                    source).
  289.  
  290.        AARRGGIINNDD      The  index  in  AARRGGVV of the current file being
  291.                    processed.
  292.  
  293.        AARRGGVV        Array of command line arguments. The array  is
  294.                    indexed  from  0  to  AARRGGCC  -  1.  Dynamically
  295.                    changing the contents of AARRGGVV can control  the
  296.                    files used for data.
  297.  
  298.        CCOONNVVFFMMTT     The  conversion format for numbers, ""%%..66gg"", by
  299.                    default.
  300.  
  301.        EENNVVIIRROONN     An array containing the values of the  current
  302.                    environment.   The  array  is  indexed  by the
  303.                    environment variables, each element being  the
  304.                    value  of that variable (e.g., EENNVVIIRROONN[[""HHOOMMEE""]]
  305.                    might be //uu//aarrnnoolldd).  Changing this array does
  306.                    not  affect  the  environment seen by programs
  307.                    which _g_a_w_k spawns via redirection or the  ssyyss--
  308.                    tteemm(())  function.  (This may change in a future
  309.                    version of _g_a_w_k.)
  310.  
  311.        EERRRRNNOO       If a system error occurs either doing a  redi-
  312.                    rection  for  ggeettlliinnee,  during a read for ggeett--
  313.                    lliinnee, or during a cclloossee, then EERRRRNNOO will  con-
  314.                    tain a string describing the error.
  315.  
  316.        FFIIEELLDDWWIIDDTTHHSS A  white-space  separated list of fieldwidths.
  317.                    When set, _g_a_w_k parses the input into fields of
  318.                    fixed width, instead of using the value of the
  319.                    FFSS variable as the field separator.  The fixed
  320.                    field  width  facility  is still experimental;
  321.                    expect the semantics to change as _g_a_w_k evolves
  322.                    over time.
  323.  
  324.        FFIILLEENNAAMMEE    The  name  of  the  current input file.  If no
  325.  
  326.  
  327.  
  328. Free Software Foundation   Apr 15 1993                          5
  329.  
  330.  
  331.  
  332.  
  333.  
  334. GAWK(1)                  Utility Commands                 GAWK(1)
  335.  
  336.  
  337.                    files are specified on the command  line,  the
  338.                    value of FFIILLEENNAAMMEE is ``-''.
  339.  
  340.        FFNNRR         The  input  record number in the current input
  341.                    file.
  342.  
  343.        FFSS          The input field separator, a blank by default.
  344.  
  345.        IIGGNNOORREECCAASSEE  Controls  the  case-sensitivity of all regular
  346.                    expression operations.  If  IIGGNNOORREECCAASSEE  has  a
  347.                    non-zero   value,  then  pattern  matching  in
  348.                    rules,  field  splitting  with   FFSS,   regular
  349.                    expression  matching  with  ~~  and !!~~, and the
  350.                    ggssuubb(()), iinnddeexx(()), mmaattcchh(()), sspplliitt(()),  and  ssuubb(())
  351.                    pre-defined  functions  will  all  ignore case
  352.                    when  doing  regular  expression   operations.
  353.                    Thus, if IIGGNNOORREECCAASSEE is not equal to zero, //aaBB//
  354.                    matches all of the strings ""aabb"",  ""aaBB"",  ""AAbb"",
  355.                    and ""AABB"".  As with all AWK variables, the ini-
  356.                    tial value of IIGGNNOORREECCAASSEE is zero, so all regu-
  357.                    lar  expression  operations are normally case-
  358.                    sensitive.
  359.  
  360.        NNFF          The number of  fields  in  the  current  input
  361.                    record.
  362.  
  363.        NNRR          The total number of input records seen so far.
  364.  
  365.        OOFFMMTT        The output  format  for  numbers,  ""%%..66gg"",  by
  366.                    default.
  367.  
  368.        OOFFSS         The   output   field  separator,  a  blank  by
  369.                    default.
  370.  
  371.        OORRSS         The output record separator, by default a new-
  372.                    line.
  373.  
  374.        RRSS          The  input record separator, by default a new-
  375.                    line.  RRSS is  exceptional  in  that  only  the
  376.                    first  character  of  its string value is used
  377.                    for separating records.  (This  will  probably
  378.                    change in a future release of _g_a_w_k.)  If RRSS is
  379.                    set to the null string, then records are sepa-
  380.                    rated  by  blank lines.  When RRSS is set to the
  381.                    null string, then the newline character always
  382.                    acts  as  a  field  separator,  in addition to
  383.                    whatever value FFSS may have.
  384.  
  385.        RRSSTTAARRTT      The index of the first  character  matched  by
  386.                    mmaattcchh(()); 0 if no match.
  387.  
  388.        RRLLEENNGGTTHH     The  length  of the string matched by mmaattcchh(());
  389.                    -1 if no match.
  390.  
  391.  
  392.  
  393.  
  394. Free Software Foundation   Apr 15 1993                          6
  395.  
  396.  
  397.  
  398.  
  399.  
  400. GAWK(1)                  Utility Commands                 GAWK(1)
  401.  
  402.  
  403.        SSUUBBSSEEPP      The character used to separate  multiple  sub-
  404.                    scripts  in array elements, by default ""\\003344"".
  405.  
  406.    AArrrraayyss
  407.        Arrays are subscripted with an expression  between  square
  408.        brackets  ([[  and  ]]).  If the expression is an expression
  409.        list (_e_x_p_r, _e_x_p_r ...)   then  the  array  subscript  is  a
  410.        string  consisting  of  the  concatenation of the (string)
  411.        value of each expression, separated by the  value  of  the
  412.        SSUUBBSSEEPP variable.  This facility is used to simulate multi-
  413.        ply dimensioned arrays. For example:
  414.  
  415.               ii == ""AA"" ;; jj == ""BB"" ;; kk == ""CC""
  416.               xx[[ii,, jj,, kk]] == ""hheelllloo,, wwoorrlldd\\nn""
  417.  
  418.        assigns the string ""hheelllloo,, wwoorrlldd\\nn"" to the element of  the
  419.        array  xx which is indexed by the string ""AA\\003344BB\\003344CC"". All
  420.        arrays in AWK are associative, i.e. indexed by string val-
  421.        ues.
  422.  
  423.        The  special  operator  iinn  may  be used in an iiff or wwhhiillee
  424.        statement to see if an array has an index consisting of  a
  425.        particular value.
  426.  
  427.               iiff ((vvaall iinn aarrrraayy))
  428.                    pprriinntt aarrrraayy[[vvaall]]
  429.  
  430.        If the array has multiple subscripts, use ((ii,, jj)) iinn aarrrraayy.
  431.  
  432.        The iinn construct may also be used in a ffoorr loop to iterate
  433.        over all the elements of an array.
  434.  
  435.        An  element  may be deleted from an array using the ddeelleettee
  436.        statement.
  437.  
  438.    VVaarriiaabbllee TTyyppiinngg AAnndd CCoonnvveerrssiioonn
  439.        Variables and fields may be (floating point)  numbers,  or
  440.        strings,  or  both.  How the value of a variable is inter-
  441.        preted depends upon its context.  If  used  in  a  numeric
  442.        expression,  it  will be treated as a number, if used as a
  443.        string it will be treated as a string.
  444.  
  445.        To force a variable to be treated as a number,  add  0  to
  446.        it;  to force it to be treated as a string, concatenate it
  447.        with the null string.
  448.  
  449.        When a string must be converted to a number,  the  conver-
  450.        sion is accomplished using _a_t_o_f(3).  A number is converted
  451.        to a string by using the value  of  CCOONNVVFFMMTT  as  a  format
  452.        string for _s_p_r_i_n_t_f(3), with the numeric value of the vari-
  453.        able as the argument.  However, even though all numbers in
  454.        AWK  are  floating-point,  integral values are _a_l_w_a_y_s con-
  455.        verted as integers.  Thus, given
  456.  
  457.  
  458.  
  459.  
  460. Free Software Foundation   Apr 15 1993                          7
  461.  
  462.  
  463.  
  464.  
  465.  
  466. GAWK(1)                  Utility Commands                 GAWK(1)
  467.  
  468.  
  469.               CCOONNVVFFMMTT == ""%%22..22ff""
  470.               aa == 1122
  471.               bb == aa """"
  472.  
  473.        the variable bb has a value of ""1122"" and not ""1122..0000"".
  474.  
  475.        _G_a_w_k performs comparisons as follows: If two variables are
  476.        numeric,  they  are compared numerically.  If one value is
  477.        numeric and the  other  has  a  string  value  that  is  a
  478.        ``numeric string,'' then comparisons are also done numeri-
  479.        cally.  Otherwise, the numeric value  is  converted  to  a
  480.        string  and a string comparison is performed.  Two strings
  481.        are compared, of course, as  strings.   According  to  the
  482.        POSIX standard, even if two strings are numeric strings, a
  483.        numeric comparison is performed.  However, this is clearly
  484.        incorrect, and _g_a_w_k does not do this.
  485.  
  486.        Uninitialized  variables  have the numeric value 0 and the
  487.        string value "" (the null, or empty, string).
  488.  
  489. PPAATTTTEERRNNSS AANNDD AACCTTIIOONNSS
  490.        AWK is a line oriented language. The pattern comes  first,
  491.        and  then  the action. Action statements are enclosed in {{
  492.        and }}.  Either the pattern may be missing, or  the  action
  493.        may  be  missing, but, of course, not both. If the pattern
  494.        is missing, the action will be executed for  every  single
  495.        line of input.  A missing action is equivalent to
  496.  
  497.               {{ pprriinntt }}
  498.  
  499.        which prints the entire line.
  500.  
  501.        Comments  begin  with  the  ``#''  character, and continue
  502.        until the end of the line.  Blank lines  may  be  used  to
  503.        separate  statements.   Normally,  a statement ends with a
  504.        newline, however, this is not the case for lines ending in
  505.        a  ``,'',  ``{'',  ``?'', ``:'', ``&&'', or ``||''.  Lines
  506.        ending in ddoo or eellssee also have their statements  automati-
  507.        cally  continued on the following line.  In other cases, a
  508.        line can be continued by ending it with a ``\'', in  which
  509.        case the newline will be ignored.
  510.  
  511.        Multiple  statements  may be put on one line by separating
  512.        them with a ``;''.  This applies to  both  the  statements
  513.        within the action part of a pattern-action pair (the usual
  514.        case), and to the pattern-action statements themselves.
  515.  
  516.    PPaatttteerrnnss
  517.        AWK patterns may be one of the following:
  518.  
  519.               BBEEGGIINN
  520.               EENNDD
  521.               //_r_e_g_u_l_a_r _e_x_p_r_e_s_s_i_o_n//
  522.               _r_e_l_a_t_i_o_n_a_l _e_x_p_r_e_s_s_i_o_n
  523.  
  524.  
  525.  
  526. Free Software Foundation   Apr 15 1993                          8
  527.  
  528.  
  529.  
  530.  
  531.  
  532. GAWK(1)                  Utility Commands                 GAWK(1)
  533.  
  534.  
  535.               _p_a_t_t_e_r_n &&&& _p_a_t_t_e_r_n
  536.               _p_a_t_t_e_r_n |||| _p_a_t_t_e_r_n
  537.               _p_a_t_t_e_r_n ?? _p_a_t_t_e_r_n :: _p_a_t_t_e_r_n
  538.               ((_p_a_t_t_e_r_n))
  539.               !! _p_a_t_t_e_r_n
  540.               _p_a_t_t_e_r_n_1,, _p_a_t_t_e_r_n_2
  541.  
  542.        BBEEGGIINN and EENNDD are two special kinds of patterns which  are
  543.        not  tested  against  the  input.  The action parts of all
  544.        BBEEGGIINN patterns are merged as if  all  the  statements  had
  545.        been  written  in  a single BBEEGGIINN block. They are executed
  546.        before any of the input is read. Similarly,  all  the  EENNDD
  547.        blocks  are  merged,  and  executed  when all the input is
  548.        exhausted (or when an eexxiitt statement is executed).   BBEEGGIINN
  549.        and EENNDD patterns cannot be combined with other patterns in
  550.        pattern expressions.  BBEEGGIINN and EENNDD patterns  cannot  have
  551.        missing action parts.
  552.  
  553.        For  //_r_e_g_u_l_a_r  _e_x_p_r_e_s_s_i_o_n// patterns, the associated state-
  554.        ment is executed for each input line that matches the reg-
  555.        ular  expression.   Regular  expressions  are  the same as
  556.        those in _e_g_r_e_p(1), and are summarized below.
  557.  
  558.        A _r_e_l_a_t_i_o_n_a_l _e_x_p_r_e_s_s_i_o_n  may  use  any  of  the  operators
  559.        defined  below in the section on actions.  These generally
  560.        test whether certain fields match certain regular  expres-
  561.        sions.
  562.  
  563.        The  &&&&, ||||, and !!  operators are logical AND, logical OR,
  564.        and logical NOT, respectively, as in C.   They  do  short-
  565.        circuit evaluation, also as in C, and are used for combin-
  566.        ing more primitive pattern expressions. As  in  most  lan-
  567.        guages,  parentheses  may  be  used to change the order of
  568.        evaluation.
  569.  
  570.        The ??:: operator is like the same operator  in  C.  If  the
  571.        first pattern is true then the pattern used for testing is
  572.        the second pattern, otherwise it is the third. Only one of
  573.        the second and third patterns is evaluated.
  574.  
  575.        The  _p_a_t_t_e_r_n_1,,  _p_a_t_t_e_r_n_2 form of an expression is called a
  576.        range pattern.  It matches all input records starting with
  577.        a  line  that  matches  _p_a_t_t_e_r_n_1,  and  continuing until a
  578.        record that matches _p_a_t_t_e_r_n_2, inclusive. It does not  com-
  579.        bine with any other sort of pattern expression.
  580.  
  581.    RReegguullaarr EExxpprreessssiioonnss
  582.        Regular  expressions are the extended kind found in _e_g_r_e_p.
  583.        They are composed of characters as follows:
  584.  
  585.        _c          matches the non-metacharacter _c.
  586.  
  587.        _\_c         matches the literal character _c.
  588.  
  589.  
  590.  
  591.  
  592. Free Software Foundation   Apr 15 1993                          9
  593.  
  594.  
  595.  
  596.  
  597.  
  598. GAWK(1)                  Utility Commands                 GAWK(1)
  599.  
  600.  
  601.        ..          matches any character except newline.
  602.  
  603.        ^^          matches the beginning of a line or a string.
  604.  
  605.        $$          matches the end of a line or a string.
  606.  
  607.        [[_a_b_c_._._.]]   character class, matches any of the  characters
  608.                   _a_b_c_._._..
  609.  
  610.        [[^^_a_b_c_._._.]]  negated  character class, matches any character
  611.                   except _a_b_c_._._.  and newline.
  612.  
  613.        _r_1||_r_2      alternation: matches either _r_1 or _r_2.
  614.  
  615.        _r_1_r_2       concatenation: matches _r_1, and then _r_2.
  616.  
  617.        _r++         matches one or more _r's.
  618.  
  619.        _r**         matches zero or more _r's.
  620.  
  621.        _r??         matches zero or one _r's.
  622.  
  623.        ((_r))        grouping: matches _r.
  624.  
  625.        The escape sequences that are valid  in  string  constants
  626.        (see below) are also legal in regular expressions.
  627.  
  628.    AAccttiioonnss
  629.        Action statements are enclosed in braces, {{ and }}.  Action
  630.        statements consist of the usual  assignment,  conditional,
  631.        and looping statements found in most languages. The opera-
  632.        tors,  control  statements,  and  input/output  statements
  633.        available are patterned after those in C.
  634.  
  635.    OOppeerraattoorrss
  636.        The  operators  in AWK, in order of increasing precedence,
  637.        are
  638.  
  639.  
  640.        == ++== --==
  641.        **== //== %%== ^^== Assignment. Both absolute  assignment  ((_v_a_r  ==
  642.                    _v_a_l_u_e))   and  operator-assignment  (the  other
  643.                    forms) are supported.
  644.  
  645.        ??::          The C conditional  expression.  This  has  the
  646.                    form  _e_x_p_r_1 ?? _e_x_p_r_2 :: _e_x_p_r_3. If _e_x_p_r_1 is true,
  647.                    the value of the expression is  _e_x_p_r_2,  other-
  648.                    wise it is _e_x_p_r_3.  Only one of _e_x_p_r_2 and _e_x_p_r_3
  649.                    is evaluated.
  650.  
  651.        ||||          Logical OR.
  652.  
  653.        &&&&          Logical AND.
  654.  
  655.  
  656.  
  657.  
  658. Free Software Foundation   Apr 15 1993                         10
  659.  
  660.  
  661.  
  662.  
  663.  
  664. GAWK(1)                  Utility Commands                 GAWK(1)
  665.  
  666.  
  667.        ~~ !!~~        Regular  expression  match,   negated   match.
  668.                    NNOOTTEE:: Do not use a constant regular expression
  669.                    (//ffoooo//) on the left-hand side of a  ~~  or  !!~~.
  670.                    Only  use  one  on  the  right-hand side.  The
  671.                    expression //ffoooo// ~~ _e_x_p has the same meaning as
  672.                    (((($$00  ~~  //ffoooo//))  ~~  _e_x_p)).  This is usually _n_o_t
  673.                    what was intended.
  674.  
  675.        << >>
  676.        <<== >>==
  677.        !!== ====       The regular relational operators.
  678.  
  679.        _b_l_a_n_k       String concatenation.
  680.  
  681.        ++ --         Addition and subtraction.
  682.  
  683.        ** // %%       Multiplication, division, and modulus.
  684.  
  685.        ++ -- !!       Unary plus, unary minus, and logical negation.
  686.  
  687.        ^^           Exponentiation  (****  may also be used, and ****==
  688.                    for the assignment operator).
  689.  
  690.        ++++ ----       Increment and decrement, both prefix and post-
  691.                    fix.
  692.  
  693.        $$           Field reference.
  694.  
  695.    CCoonnttrrooll SSttaatteemmeennttss
  696.        The control statements are as follows:
  697.  
  698.               iiff ((_c_o_n_d_i_t_i_o_n)) _s_t_a_t_e_m_e_n_t [ eellssee _s_t_a_t_e_m_e_n_t ]
  699.               wwhhiillee ((_c_o_n_d_i_t_i_o_n)) _s_t_a_t_e_m_e_n_t
  700.               ddoo _s_t_a_t_e_m_e_n_t wwhhiillee ((_c_o_n_d_i_t_i_o_n))
  701.               ffoorr ((_e_x_p_r_1;; _e_x_p_r_2;; _e_x_p_r_3)) _s_t_a_t_e_m_e_n_t
  702.               ffoorr ((_v_a_r iinn _a_r_r_a_y)) _s_t_a_t_e_m_e_n_t
  703.               bbrreeaakk
  704.               ccoonnttiinnuuee
  705.               ddeelleettee _a_r_r_a_y[[_i_n_d_e_x]]
  706.               eexxiitt [ _e_x_p_r_e_s_s_i_o_n ]
  707.               {{ _s_t_a_t_e_m_e_n_t_s }}
  708.  
  709.    II//OO SSttaatteemmeennttss
  710.        The input/output statements are as follows:
  711.  
  712.  
  713.        cclloossee((_f_i_l_e_n_a_m_e))       Close file (or pipe, see below).
  714.  
  715.        ggeettlliinnee               Set  $$00  from next input record; set
  716.                              NNFF, NNRR, FFNNRR.
  717.  
  718.        ggeettlliinnee <<_f_i_l_e         Set $$00 from next record of _f_i_l_e; set
  719.                              NNFF.
  720.  
  721.  
  722.  
  723.  
  724. Free Software Foundation   Apr 15 1993                         11
  725.  
  726.  
  727.  
  728.  
  729.  
  730. GAWK(1)                  Utility Commands                 GAWK(1)
  731.  
  732.  
  733.        ggeettlliinnee _v_a_r           Set  _v_a_r from next input record; set
  734.                              NNFF, FFNNRR.
  735.  
  736.        ggeettlliinnee _v_a_r <<_f_i_l_e     Set _v_a_r from next record of _f_i_l_e.
  737.  
  738.        nneexxtt                  Stop processing  the  current  input
  739.                              record.  The  next  input  record is
  740.                              read and processing starts over with
  741.                              the  first  pattern  in the AWK pro-
  742.                              gram. If the end of the  input  data
  743.                              is  reached,  the  EENNDD  block(s), if
  744.                              any, are executed.
  745.  
  746.        nneexxtt ffiillee             Stop processing  the  current  input
  747.                              file.   The  next  input record read
  748.                              comes  from  the  next  input  file.
  749.                              FFIILLEENNAAMMEE is updated, FFNNRR is reset to
  750.                              1, and processing starts  over  with
  751.                              the  first  pattern  in the AWK pro-
  752.                              gram. If the end of the  input  data
  753.                              is  reached,  the  EENNDD  block(s), if
  754.                              any, are executed.
  755.  
  756.        pprriinntt                 Prints the current record.
  757.  
  758.        pprriinntt _e_x_p_r_-_l_i_s_t       Prints expressions.
  759.  
  760.        pprriinntt _e_x_p_r_-_l_i_s_t >>_f_i_l_e Prints expressions on _f_i_l_e.
  761.  
  762.        pprriinnttff _f_m_t_, _e_x_p_r_-_l_i_s_t Format and print.
  763.  
  764.        pprriinnttff _f_m_t_, _e_x_p_r_-_l_i_s_t >>_f_i_l_e
  765.                              Format and print on _f_i_l_e.
  766.  
  767.        ssyysstteemm((_c_m_d_-_l_i_n_e))      Execute the  command  _c_m_d_-_l_i_n_e,  and
  768.                              return  the  exit status.  (This may
  769.                              not be available on  non-POSIX  sys-
  770.                              tems.)
  771.  
  772.        Other  input/output  redirections  are  also  allowed. For
  773.        pprriinntt and pprriinnttff, >>>>_f_i_l_e appends output to the _f_i_l_e, while
  774.        || _c_o_m_m_a_n_d writes on a pipe.  In a similar fashion, _c_o_m_m_a_n_d
  775.        || ggeettlliinnee pipes into ggeettlliinnee.  GGeettlliinnee will  return  0  on
  776.        end of file, and -1 on an error.
  777.  
  778.    TThhee _p_r_i_n_t_f SSttaatteemmeenntt
  779.        The  AWK  versions  of  the pprriinnttff statement and sspprriinnttff(())
  780.        function (see below) accept the following conversion spec-
  781.        ification formats:
  782.  
  783.        %%cc     An ASCII character.  If the argument used for %%cc is
  784.               numeric, it is treated as a character and  printed.
  785.               Otherwise,  the argument is assumed to be a string,
  786.               and the only first  character  of  that  string  is
  787.  
  788.  
  789.  
  790. Free Software Foundation   Apr 15 1993                         12
  791.  
  792.  
  793.  
  794.  
  795.  
  796. GAWK(1)                  Utility Commands                 GAWK(1)
  797.  
  798.  
  799.               printed.
  800.  
  801.        %%dd     A decimal number (the integer part).
  802.  
  803.        %%ii     Just like %%dd.
  804.  
  805.        %%ee     A    floating    point    number    of   the   form
  806.               [[--]]dd..ddddddddddddEE[[++--]]dddd.
  807.  
  808.        %%ff     A floating point number of the form  [[--]]dddddd..dddddddddddd.
  809.  
  810.        %%gg     Use  ee  or ff conversion, whichever is shorter, with
  811.               nonsignificant zeros suppressed.
  812.  
  813.        %%oo     An unsigned octal number (again, an integer).
  814.  
  815.        %%ss     A character string.
  816.  
  817.        %%xx     An unsigned hexadecimal number (an integer).
  818.  
  819.        %%XX     Like %%xx, but using AABBCCDDEEFF instead of aabbccddeeff.
  820.  
  821.        %%%%     A single %% character; no argument is converted.
  822.  
  823.        There are optional, additional  parameters  that  may  lie
  824.        between the %% and the control letter:
  825.  
  826.        --      The  expression should be left-justified within its
  827.               field.
  828.  
  829.        _w_i_d_t_h  The field should be padded to this  width.  If  the
  830.               number  has  a leading zero, then the field will be
  831.               padded with zeros.  Otherwise  it  is  padded  with
  832.               blanks.
  833.  
  834.        .._p_r_e_c  A number indicating the maximum width of strings or
  835.               digits to the right of the decimal point.
  836.  
  837.        The dynamic _w_i_d_t_h and _p_r_e_c  capabilities  of  the  ANSI  C
  838.        pprriinnttff(())  routines  are supported.  A ** in place of either
  839.        the wwiiddtthh or pprreecc specifications will cause  their  values
  840.        to be taken from the argument list to pprriinnttff or sspprriinnttff(()).
  841.  
  842.    SSppeecciiaall FFiillee NNaammeess
  843.        When doing I/O redirection from  either  pprriinntt  or  pprriinnttff
  844.        into  a  file, or via ggeettlliinnee from a file, _g_a_w_k recognizes
  845.        certain special  filenames  internally.   These  filenames
  846.        allow  access  to  open  file  descriptors  inherited from
  847.        _g_a_w_k's parent process (usually the shell).  Other  special
  848.        filenames  provide  access  information  about the running
  849.        ggaawwkk process.  The filenames are:
  850.  
  851.        //ddeevv//ppiidd    Reading this file returns the  process  ID  of
  852.                    the  current  process,  in decimal, terminated
  853.  
  854.  
  855.  
  856. Free Software Foundation   Apr 15 1993                         13
  857.  
  858.  
  859.  
  860.  
  861.  
  862. GAWK(1)                  Utility Commands                 GAWK(1)
  863.  
  864.  
  865.                    with a newline.
  866.  
  867.        //ddeevv//ppppiidd   Reading this file returns the  parent  process
  868.                    ID  of the current process, in decimal, termi-
  869.                    nated with a newline.
  870.  
  871.        //ddeevv//ppggrrppiidd Reading this file returns the process group ID
  872.                    of the current process, in decimal, terminated
  873.                    with a newline.
  874.  
  875.        //ddeevv//uusseerr   Reading this file returns a single record ter-
  876.                    minated  with a newline.  The fields are sepa-
  877.                    rated with blanks.  $$11 is  the  value  of  the
  878.                    _g_e_t_u_i_d(2)  system call, $$22 is the value of the
  879.                    _g_e_t_e_u_i_d(2) system call, $$33 is the value of the
  880.                    _g_e_t_g_i_d(2)  system call, and $$44 is the value of
  881.                    the _g_e_t_e_g_i_d(2) system call.  If there are  any
  882.                    additional  fields,  they  are  the  group IDs
  883.                    returned by  _g_e_t_g_r_o_u_p_s(2).   (Multiple  groups
  884.                    may not be supported on all systems.)
  885.  
  886.        //ddeevv//ssttddiinn  The standard input.
  887.  
  888.        //ddeevv//ssttddoouutt The standard output.
  889.  
  890.        //ddeevv//ssttddeerrrr The standard error output.
  891.  
  892.        //ddeevv//ffdd//_n   The   file   associated  with  the  open  file
  893.                    descriptor _n.
  894.  
  895.        These are particularly  useful  for  error  messages.  For
  896.        example:
  897.  
  898.               pprriinntt ""YYoouu bblleeww iitt!!"" >> ""//ddeevv//ssttddeerrrr""
  899.  
  900.        whereas you would otherwise have to use
  901.  
  902.               pprriinntt ""YYoouu bblleeww iitt!!"" || ""ccaatt 11>>&&22""
  903.  
  904.        These  file  names may also be used on the command line to
  905.        name data files.
  906.  
  907.    NNuummeerriicc FFuunnccttiioonnss
  908.        AWK has the following pre-defined arithmetic functions:
  909.  
  910.  
  911.        aattaann22((_y,, _x)) returns the arctangent of _y_/_x in radians.
  912.  
  913.        ccooss((_e_x_p_r))   returns the cosine in radians.
  914.  
  915.        eexxpp((_e_x_p_r))   the exponential function.
  916.  
  917.        iinntt((_e_x_p_r))   truncates to integer.
  918.  
  919.  
  920.  
  921.  
  922. Free Software Foundation   Apr 15 1993                         14
  923.  
  924.  
  925.  
  926.  
  927.  
  928. GAWK(1)                  Utility Commands                 GAWK(1)
  929.  
  930.  
  931.        lloogg((_e_x_p_r))   the natural logarithm function.
  932.  
  933.        rraanndd(())      returns a random number between 0 and 1.
  934.  
  935.        ssiinn((_e_x_p_r))   returns the sine in radians.
  936.  
  937.        ssqqrrtt((_e_x_p_r))  the square root function.
  938.  
  939.        ssrraanndd((_e_x_p_r)) use _e_x_p_r as a new seed for the  random  number
  940.                    generator. If no _e_x_p_r is provided, the time of
  941.                    day will be used.  The  return  value  is  the
  942.                    previous seed for the random number generator.
  943.  
  944.    SSttrriinngg FFuunnccttiioonnss
  945.        AWK has the following pre-defined string functions:
  946.  
  947.  
  948.        ggssuubb((_r,, _s,, _t))           for each  substring  matching  the
  949.                                regular expression _r in the string
  950.                                _t, substitute the  string  _s,  and
  951.                                return  the  number  of  substitu-
  952.                                tions.  If _t is not supplied,  use
  953.                                $$00.
  954.  
  955.        iinnddeexx((_s,, _t))             returns  the index of the string _t
  956.                                in the string _s, or 0 if _t is  not
  957.                                present.
  958.  
  959.        lleennggtthh((_s))               returns  the  length of the string
  960.                                _s, or the length of $$00 if _s is not
  961.                                supplied.
  962.  
  963.        mmaattcchh((_s,, _r))             returns  the  position  in _s where
  964.                                the regular expression  _r  occurs,
  965.                                or 0 if _r is not present, and sets
  966.                                the values of RRSSTTAARRTT and  RRLLEENNGGTTHH.
  967.  
  968.        sspplliitt((_s,, _a,, _r))          splits the string _s into the array
  969.                                _a on the regular expression _r, and
  970.                                returns the number of fields. If _r
  971.                                is omitted, FFSS is used instead.
  972.  
  973.        sspprriinnttff((_f_m_t,, _e_x_p_r_-_l_i_s_t)) prints _e_x_p_r_-_l_i_s_t according to _f_m_t,
  974.                                and  returns the resulting string.
  975.  
  976.        ssuubb((_r,, _s,, _t))            just like  ggssuubb(()),  but  only  the
  977.                                first    matching   substring   is
  978.                                replaced.
  979.  
  980.        ssuubbssttrr((_s,, _i,, _n))         returns the _n-character  substring
  981.                                of _s starting at _i.  If _n is omit-
  982.                                ted, the rest of _s is used.
  983.  
  984.        ttoolloowweerr((_s_t_r))            returns a copy of the string  _s_t_r,
  985.  
  986.  
  987.  
  988. Free Software Foundation   Apr 15 1993                         15
  989.  
  990.  
  991.  
  992.  
  993.  
  994. GAWK(1)                  Utility Commands                 GAWK(1)
  995.  
  996.  
  997.                                with all the upper-case characters
  998.                                in _s_t_r translated to their  corre-
  999.                                sponding  lower-case counterparts.
  1000.                                Non-alphabetic characters are left
  1001.                                unchanged.
  1002.  
  1003.        ttoouuppppeerr((_s_t_r))            returns  a copy of the string _s_t_r,
  1004.                                with all the lower-case characters
  1005.                                in  _s_t_r translated to their corre-
  1006.                                sponding upper-case  counterparts.
  1007.                                Non-alphabetic characters are left
  1008.                                unchanged.
  1009.  
  1010.    TTiimmee FFuunnccttiioonnss
  1011.        Since one of the primary uses of AWK programs is  process-
  1012.        ing  log  files  that contain time stamp information, _g_a_w_k
  1013.        provides the following two functions  for  obtaining  time
  1014.        stamps and formatting them.
  1015.  
  1016.  
  1017.        ssyyssttiimmee(()) returns the current time of day as the number of
  1018.                  seconds since the Epoch (Midnight  UTC,  January
  1019.                  1, 1970 on POSIX systems).
  1020.  
  1021.        ssttrrffttiimmee((_f_o_r_m_a_t, _t_i_m_e_s_t_a_m_p))
  1022.                  formats _t_i_m_e_s_t_a_m_p according to the specification
  1023.                  in _f_o_r_m_a_t_.  The _t_i_m_e_s_t_a_m_p should be of the  same
  1024.                  form  as returned by ssyyssttiimmee(()).  If _t_i_m_e_s_t_a_m_p is
  1025.                  missing, the current time of day is  used.   See
  1026.                  the specification for the ssttrrffttiimmee(()) function in
  1027.                  ANSI C for the format conversions that are guar-
  1028.                  anteed to be available.  A public-domain version
  1029.                  of _s_t_r_f_t_i_m_e(3) and a man page for it are shipped
  1030.                  with  _g_a_w_k;  if  that  version was used to build
  1031.                  _g_a_w_k, then all of the conversions  described  in
  1032.                  that man page are available to _g_a_w_k_.
  1033.  
  1034.    SSttrriinngg CCoonnssttaannttss
  1035.        String  constants  in  AWK  are  sequences  of  characters
  1036.        enclosed between double quotes (""). Within  strings,  cer-
  1037.        tain _e_s_c_a_p_e _s_e_q_u_e_n_c_e_s are recognized, as in C. These are:
  1038.  
  1039.  
  1040.        \\\\   A literal backslash.
  1041.  
  1042.        \\aa   The  ``alert'' character; usually the ASCII BEL char-
  1043.             acter.
  1044.  
  1045.        \\bb   backspace.
  1046.  
  1047.        \\ff   form-feed.
  1048.  
  1049.        \\nn   new line.
  1050.  
  1051.  
  1052.  
  1053.  
  1054. Free Software Foundation   Apr 15 1993                         16
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. GAWK(1)                  Utility Commands                 GAWK(1)
  1061.  
  1062.  
  1063.        \\rr   carriage return.
  1064.  
  1065.        \\tt   horizontal tab.
  1066.  
  1067.        \\vv   vertical tab.
  1068.  
  1069.        \\xx_h_e_x _d_i_g_i_t_s
  1070.             The character represented by the string of  hexadeci-
  1071.             mal  digits following the \\xx.  As in ANSI C, all fol-
  1072.             lowing hexadecimal digits are considered part of  the
  1073.             escape  sequence.  (This feature should tell us some-
  1074.             thing about language  design  by  committee.)   E.g.,
  1075.             "\x1B" is the ASCII ESC (escape) character.
  1076.  
  1077.        \\_d_d_d The  character  represented by the 1-, 2-, or 3-digit
  1078.             sequence of octal digits. E.g. "\033"  is  the  ASCII
  1079.             ESC (escape) character.
  1080.  
  1081.        \\_c   The literal character _c.
  1082.  
  1083.        The escape sequences may also be used inside constant reg-
  1084.        ular expressions (e.g., //[[ \\tt\\ff\\nn\\rr\\vv]]// matches whitespace
  1085.        characters).
  1086.  
  1087. FFUUNNCCTTIIOONNSS
  1088.        Functions in AWK are defined as follows:
  1089.  
  1090.               ffuunnccttiioonn _n_a_m_e((_p_a_r_a_m_e_t_e_r _l_i_s_t)) {{ _s_t_a_t_e_m_e_n_t_s }}
  1091.  
  1092.        Functions  are executed when called from within the action
  1093.        parts of regular pattern-action statements. Actual parame-
  1094.        ters supplied in the function call are used to instantiate
  1095.        the formal parameters declared in  the  function.   Arrays
  1096.        are  passed  by  reference,  other variables are passed by
  1097.        value.
  1098.  
  1099.        Since functions were not originally part of the  AWK  lan-
  1100.        guage, the provision for local variables is rather clumsy:
  1101.        They are declared as extra  parameters  in  the  parameter
  1102.        list.  The  convention is to separate local variables from
  1103.        real parameters by extra spaces in the parameter list. For
  1104.        example:
  1105.  
  1106.               ffuunnccttiioonn  ff((pp,, qq,,     aa,, bb)) {{ ## aa && bb aarree llooccaall
  1107.                              .......... }}
  1108.  
  1109.               //aabbcc//     {{ ...... ;; ff((11,, 22)) ;; ...... }}
  1110.  
  1111.        The  left  parenthesis  in  a function call is required to
  1112.        immediately follow the function name, without  any  inter-
  1113.        vening  white space.  This is to avoid a syntactic ambigu-
  1114.        ity with the  concatenation  operator.   This  restriction
  1115.        does not apply to the built-in functions listed above.
  1116.  
  1117.  
  1118.  
  1119.  
  1120. Free Software Foundation   Apr 15 1993                         17
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. GAWK(1)                  Utility Commands                 GAWK(1)
  1127.  
  1128.  
  1129.        Functions may call each other and may be recursive.  Func-
  1130.        tion parameters used as local variables are initialized to
  1131.        the  null string and the number zero upon function invoca-
  1132.        tion.
  1133.  
  1134.        The word ffuunncc may be used in place of ffuunnccttiioonn.
  1135.  
  1136. EEXXAAMMPPLLEESS
  1137.        Print and sort the login names of all users:
  1138.  
  1139.             BBEEGGIINN     {{ FFSS == ""::"" }}
  1140.                  {{ pprriinntt $$11 || ""ssoorrtt"" }}
  1141.  
  1142.        Count lines in a file:
  1143.  
  1144.                  {{ nnlliinneess++++ }}
  1145.             EENNDD  {{ pprriinntt nnlliinneess }}
  1146.  
  1147.        Precede each line by its number in the file:
  1148.  
  1149.             {{ pprriinntt FFNNRR,, $$00 }}
  1150.  
  1151.        Concatenate and line number (a variation on a theme):
  1152.  
  1153.             {{ pprriinntt NNRR,, $$00 }}
  1154.  
  1155. SSEEEE AALLSSOO
  1156.        _e_g_r_e_p(1)
  1157.  
  1158.        _T_h_e _A_W_K _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e,  Alfred  V.  Aho,  Brian  W.
  1159.        Kernighan, Peter J. Weinberger, Addison-Wesley, 1988. ISBN
  1160.        0-201-07981-X.
  1161.  
  1162.        _T_h_e _G_A_W_K _M_a_n_u_a_l, Edition 0.15, published by the Free Soft-
  1163.        ware Foundation, 1993.
  1164.  
  1165. PPOOSSIIXX CCOOMMPPAATTIIBBIILLIITTYY
  1166.        A  primary  goal  for _g_a_w_k is compatibility with the POSIX
  1167.        standard, as well as with the latest version of UNIX  _a_w_k.
  1168.        To  this end, _g_a_w_k incorporates the following user visible
  1169.        features which are not described in the AWK book, but  are
  1170.        part  of  _a_w_k  in System V Release 4, and are in the POSIX
  1171.        standard.
  1172.  
  1173.        The --vv option for assigning variables before program  exe-
  1174.        cution  starts  is  new.   The book indicates that command
  1175.        line variable assignment happens when _a_w_k would  otherwise
  1176.        open  the  argument  as  a  file, which is after the BBEEGGIINN
  1177.        block is executed.  However, in  earlier  implementations,
  1178.        when  such  an  assignment appeared before any file names,
  1179.        the assignment would happen _b_e_f_o_r_e  the  BBEEGGIINN  block  was
  1180.        run.   Applications  came  to  depend on this ``feature.''
  1181.        When _a_w_k was changed  to  match  its  documentation,  this
  1182.        option  was added to accomodate applications that depended
  1183.  
  1184.  
  1185.  
  1186. Free Software Foundation   Apr 15 1993                         18
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. GAWK(1)                  Utility Commands                 GAWK(1)
  1193.  
  1194.  
  1195.        upon the old behavior.  (This feature was agreed  upon  by
  1196.        both the AT&T and GNU developers.)
  1197.  
  1198.        The --WW option for implementation specific features is from
  1199.        the POSIX standard.
  1200.  
  1201.        When processing arguments, _g_a_w_k uses  the  special  option
  1202.        ``----''  to  signal  the end of arguments, and warns about,
  1203.        but otherwise ignores, undefined options.
  1204.  
  1205.        The AWK book does not define the return value of  ssrraanndd(()).
  1206.        The  System V Release 4 version of UNIX _a_w_k (and the POSIX
  1207.        standard) has it return the seed it was  using,  to  allow
  1208.        keeping   track  of  random  number  sequences.  Therefore
  1209.        ssrraanndd(()) in _g_a_w_k also returns its current seed.
  1210.  
  1211.        Other new features are: The use  of  multiple  --ff  options
  1212.        (from  MKS  _a_w_k); the EENNVVIIRROONN array; the \\aa, and \\vv escape
  1213.        sequences (done originally  in  _g_a_w_k  and  fed  back  into
  1214.        AT&T's);  the  ttoolloowweerr(())  and ttoouuppppeerr(()) built-in functions
  1215.        (from AT&T); and the ANSI C conversion  specifications  in
  1216.        pprriinnttff (done first in AT&T's version).
  1217.  
  1218. GGNNUU EEXXTTEENNSSIIOONNSS
  1219.        _G_a_w_k has some extensions to POSIX _a_w_k.  They are described
  1220.        in this section.  All the extensions described here can be
  1221.        disabled by invoking _g_a_w_k with the --WW ccoommppaatt option.
  1222.  
  1223.        The  following features of _g_a_w_k are not available in POSIX
  1224.        _a_w_k.
  1225.  
  1226.               o The \\xx escape sequence.
  1227.  
  1228.               o The ssyyssttiimmee(()) and ssttrrffttiimmee(()) functions.
  1229.  
  1230.               o The special file names available for I/O redirec-
  1231.                 tion are not recognized.
  1232.  
  1233.               o The AARRGGIINNDD and EERRRRNNOO variables are not special.
  1234.  
  1235.               o The  IIGGNNOORREECCAASSEE variable and its side-effects are
  1236.                 not available.
  1237.  
  1238.               o The FFIIEELLDDWWIIDDTTHHSS variable and  fixed  width  field
  1239.                 splitting.
  1240.  
  1241.               o No  path  search is performed for files named via
  1242.                 the --ff option.  Therefore the AAWWKKPPAATTHH environment
  1243.                 variable is not special.
  1244.  
  1245.               o The use of nneexxtt ffiillee to abandon processing of the
  1246.                 current input file.
  1247.  
  1248.        The AWK book does not  define  the  return  value  of  the
  1249.  
  1250.  
  1251.  
  1252. Free Software Foundation   Apr 15 1993                         19
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. GAWK(1)                  Utility Commands                 GAWK(1)
  1259.  
  1260.  
  1261.        cclloossee(())  function.   _G_a_w_k's cclloossee(()) returns the value from
  1262.        _f_c_l_o_s_e(3), or _p_c_l_o_s_e(3), when  closing  a  file  or  pipe,
  1263.        respectively.
  1264.  
  1265.        When  _g_a_w_k is invoked with the --WW ccoommppaatt option, if the _f_s
  1266.        argument to the --FF option is ``t'', then FFSS will be set to
  1267.        the  tab  character.   Since this is a rather ugly special
  1268.        case, it is not the default behavior.  This behavior  also
  1269.        does not occur if --WWppoossiixx has been specified.
  1270.  
  1271. HHIISSTTOORRIICCAALL FFEEAATTUURREESS
  1272.        There  are  two features of historical AWK implementations
  1273.        that _g_a_w_k supports.  First, it is  possible  to  call  the
  1274.        lleennggtthh(())  built-in function not only with no argument, but
  1275.        even without parentheses!  Thus,
  1276.  
  1277.               aa == lleennggtthh
  1278.  
  1279.        is the same as either of
  1280.  
  1281.               aa == lleennggtthh(())
  1282.               aa == lleennggtthh(($$00))
  1283.  
  1284.        This feature is marked  as  ``deprecated''  in  the  POSIX
  1285.        standard,  and  _g_a_w_k will issue a warning about its use if
  1286.        --WWlliinntt is specified on the command line.
  1287.  
  1288.        The other feature is the use  of  the  ccoonnttiinnuuee  statement
  1289.        outside the body of a wwhhiillee, ffoorr, or ddoo loop.  Traditional
  1290.        AWK implementations have treated such usage as  equivalent
  1291.        to  the  nneexxtt  statement.  _G_a_w_k will support this usage if
  1292.        --WWppoossiixx has not been specified.
  1293.  
  1294. BBUUGGSS
  1295.        The --FF option is not  necessary  given  the  command  line
  1296.        variable assignment feature; it remains only for backwards
  1297.        compatibility.
  1298.  
  1299.        If your system actually has support for  //ddeevv//ffdd  and  the
  1300.        associated //ddeevv//ssttddiinn, //ddeevv//ssttddoouutt, and //ddeevv//ssttddeerrrr files,
  1301.        you may get different output from _g_a_w_k than you would  get
  1302.        on  a  system  without  those files.  When _g_a_w_k interprets
  1303.        these files internally,  it  synchronizes  output  to  the
  1304.        standard  output  with  output  to //ddeevv//ssttddoouutt, while on a
  1305.        system with those files, the output is actually to differ-
  1306.        ent open files.  Caveat Emptor.
  1307.  
  1308. VVEERRSSIIOONN IINNFFOORRMMAATTIIOONN
  1309.        This man page documents _g_a_w_k, version 2.15.
  1310.  
  1311.        Starting  with  the  2.15 version of _g_a_w_k, the --cc, --VV, --CC,
  1312.        --aa, and --ee options of the 2.11 version are no longer  rec-
  1313.        ognized.
  1314.  
  1315.  
  1316.  
  1317.  
  1318. Free Software Foundation   Apr 15 1993                         20
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324. GAWK(1)                  Utility Commands                 GAWK(1)
  1325.  
  1326.  
  1327. AAUUTTHHOORRSS
  1328.        The  original  version of UNIX _a_w_k was designed and imple-
  1329.        mented  by  Alfred  Aho,  Peter  Weinberger,   and   Brian
  1330.        Kernighan  of AT&T Bell Labs. Brian Kernighan continues to
  1331.        maintain and enhance it.
  1332.  
  1333.        Paul Rubin and Jay Fenlason, of the Free Software  Founda-
  1334.        tion,  wrote _g_a_w_k, to be compatible with the original ver-
  1335.        sion of _a_w_k distributed in  Seventh  Edition  UNIX.   John
  1336.        Woods  contributed  a number of bug fixes.  David Trueman,
  1337.        with contributions from Arnold Robbins, made _g_a_w_k compati-
  1338.        ble with the new version of UNIX _a_w_k.
  1339.  
  1340.        The  initial  DOS  port  was done by Conrad Kwok and Scott
  1341.        Garfinkle.  Scott Deifik is the  current  DOS  maintainer.
  1342.        Pat  Rankin did the port to VMS, and Michal Jaegermann did
  1343.        the port to the Atari ST.
  1344.  
  1345. AACCKKNNOOWWLLEEDDGGEEMMEENNTTSS
  1346.        Brian Kernighan of Bell Labs provided valuable  assistance
  1347.        during testing and debugging.  We thank him.
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384. Free Software Foundation   Apr 15 1993                         21
  1385.  
  1386.  
  1387.