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