home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / gawk2.0.t.Z / gawk2.0.t / awk.doc < prev    next >
Text File  |  1989-04-06  |  31KB  |  1,717 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.       Awk - A Pattern Scanning and Processing Language
  11.                       (Second Edition)
  12.  
  13.  
  14.                        Alfred V. Aho
  15.  
  16.                      Brian W. Kernighan
  17.  
  18.                     Peter J. Weinberger
  19.  
  20.  
  21.  
  22.  
  23.                           _A_B_S_T_R_A_C_T
  24.  
  25.           _A_w_k is a  programming  language  whose  basic
  26.      operation  is  to  search  a set of files for pat-
  27.      terns, and to perform specified actions upon lines
  28.      or  fields  of  lines  which  contain instances of
  29.      those patterns.  _A_w_k makes certain data  selection
  30.      and transformation operations easy to express; for
  31.      example, the _a_w_k program
  32.  
  33.                         length > 72
  34.  
  35.      prints all input lines  whose  length  exceeds  72
  36.      characters; the program
  37.  
  38.                         NF % 2 == 0
  39.  
  40.      prints all lines with an even  number  of  fields;
  41.      and the program
  42.  
  43.                   { $1 = log($1); print }
  44.  
  45.      replaces the first field of each line by its loga-
  46.      rithm.
  47.  
  48.           _A_w_k patterns may  include  arbitrary  boolean
  49.      combinations  of  regular expressions and of rela-
  50.      tional  operators  on  strings,  numbers,  fields,
  51.      variables,   and   array  elements.   Actions  may
  52.      include the same pattern-matching constructions as
  53.      in  patterns,  as  well  as  arithmetic and string
  54.      expressions and assignments, if-else,  while,  for
  55.      statements, and multiple output streams.
  56.  
  57.           This report contains a user's guide,  a  dis-
  58.      cussion  of  the design and implementation of _a_w_k,
  59.      and some timing statistics.
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                            - ii -
  71.  
  72.  
  73. September 1, 1978
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.       Awk - A Pattern Scanning and Processing Language
  143.  
  144.                       (Second Edition)
  145.  
  146.  
  147.  
  148.                        Alfred V. Aho
  149.  
  150.  
  151.                      Brian W. Kernighan
  152.  
  153.  
  154.                     Peter J. Weinberger
  155.  
  156.  
  157.  
  158.  
  159.  
  160. _1.  _I_n_t_r_o_d_u_c_t_i_o_n
  161.  
  162.  
  163.      _A_w_k is a programming language  designed  to  make  many
  164.  
  165. common  information  retrieval  and  text manipulation tasks
  166.  
  167. easy to state and to perform.
  168.  
  169.  
  170.      The basic operation of _a_w_k is to scan a  set  of  input
  171.  
  172. lines in order, searching for lines which match any of a set
  173.  
  174. of patterns which the user has specified.  For each pattern,
  175.  
  176. an action can be specified; this action will be performed on
  177.  
  178. each line that matches the pattern.
  179.  
  180.  
  181.      Readers familiar with the UNIX|-  program  _g_r_e_p[1]  will
  182.  
  183. recognize  the approach, although in _a_w_k the patterns may be
  184.  
  185. more general than in _g_r_e_p, and the actions allowed are  more
  186.  
  187. involved  than merely printing the matching line.  For exam-
  188.  
  189. ple, the _a_w_k program
  190. -------------------------
  191. |- UNIX is a trademark of Bell Laboratories.
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                            - 2 -
  203.  
  204.  
  205.  
  206.    {print $3, $2}
  207.  
  208. prints the third and second  columns  of  a  table  in  that
  209.  
  210. order.  The program
  211.  
  212.  
  213.    $2 ~ /A|B|C/
  214.  
  215.  
  216. prints all input lines with an A, B,  or  C  in  the  second
  217.  
  218. field.  The program
  219.  
  220.  
  221.    $1 != prev     { print; prev = $1 }
  222.  
  223.  
  224. prints all lines in which the first field is different  from
  225.  
  226. the previous first field.
  227.  
  228.  
  229. _1._1.  _U_s_a_g_e
  230.  
  231.  
  232.      The command
  233.  
  234.  
  235.    awk  program  [files]
  236.  
  237.  
  238. executes the _a_w_k commands in the string program on  the  set
  239.  
  240. of  named  files,  or  on the standard input if there are no
  241.  
  242. files.  The statements can also be placed in a  file  pfile,
  243.  
  244. and executed by the command
  245.  
  246.  
  247.    awk  -f pfile  [files]
  248.  
  249.  
  250.  
  251. _1._2.  _P_r_o_g_r_a_m _S_t_r_u_c_t_u_r_e
  252.  
  253.  
  254.      An _a_w_k program is a sequence of statements of the form:
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                            - 3 -
  269.  
  270.  
  271.  
  272.         _p_a_t_t_e_r_n   { _a_c_t_i_o_n }
  273.  
  274.         _p_a_t_t_e_r_n   { _a_c_t_i_o_n }
  275.  
  276.         ...
  277.  
  278.  
  279. Each line of input is matched against each of  the  patterns
  280.  
  281. in  turn.   For  each  pattern  that matches, the associated
  282.  
  283. action is executed.  When all the patterns have been tested,
  284.  
  285. the next line is fetched and the matching starts over.
  286.  
  287.  
  288.      Either the pattern or the action may be left  out,  but
  289.  
  290. not both.  If there is no action for a pattern, the matching
  291.  
  292. line is simply copied to the output.   (Thus  a  line  which
  293.  
  294. matches  several  patterns can be printed several times.) If
  295.  
  296. there is no pattern for an action, then the action  is  per-
  297.  
  298. formed  for  every input line.  A line which matches no pat-
  299.  
  300. tern is ignored.
  301.  
  302.  
  303.      Since patterns and actions are both  optional,  actions
  304.  
  305. must  be  enclosed  in  braces to distinguish them from pat-
  306.  
  307. terns.
  308.  
  309.  
  310. _1._3.  _R_e_c_o_r_d_s _a_n_d _F_i_e_l_d_s
  311.  
  312.  
  313.      _A_w_k input is divided into ``records'' terminated  by  a
  314.  
  315. record  separator.   The  default record separator is a new-
  316.  
  317. line, so by default _a_w_k processes its  input  a  line  at  a
  318.  
  319. time.   The  number  of the current record is available in a
  320.  
  321. variable named NR.
  322.  
  323.  
  324.      Each input record is  considered  to  be  divided  into
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                            - 4 -
  335.  
  336.  
  337. ``fields.''  Fields  are normally separated by white space -
  338.  
  339. blanks or tabs -  but  the  input  field  separator  may  be
  340.  
  341. changed,  as described below.  Fields are referred to as $1,
  342.  
  343. $2, and so forth, where $1 is the first field, and $0 is the
  344.  
  345. whole  input record itself.  Fields may be assigned to.  The
  346.  
  347. number of fields in the current record  is  available  in  a
  348.  
  349. variable named NF.
  350.  
  351.  
  352.      The variables FS and RS refer to the  input  field  and
  353.  
  354. record  separators;  they  may be changed at any time to any
  355.  
  356. single character.  The optional  command-line  argument  -F_c
  357.  
  358. may also be used to set FS to the character _c.
  359.  
  360.  
  361.      If the record separator is empty, an empty  input  line
  362.  
  363. is  taken as the record separator, and blanks, tabs and new-
  364.  
  365. lines are treated as field separators.
  366.  
  367.  
  368.      The variable FILENAME contains the name of the  current
  369.  
  370. input file.
  371.  
  372.  
  373. _1._4.  _P_r_i_n_t_i_n_g
  374.  
  375.  
  376.      An action may have no pattern, in which case the action
  377.  
  378. is  executed for all lines.  The simplest action is to print
  379.  
  380. some or all of a record; this is  accomplished  by  the  _a_w_k
  381.  
  382. command print.  The _a_w_k program
  383.  
  384.  
  385.    { print }
  386.  
  387.  
  388. prints each record, thus copying the  input  to  the  output
  389.  
  390. intact.  More useful is to print a field or fields from each
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                            - 5 -
  401.  
  402.  
  403. record.  For instance,
  404.  
  405.  
  406.    print $2, $1
  407.  
  408.  
  409. prints  the  first  two  fields  in  reverse  order.   Items
  410.  
  411. separated  by  a  comma  in  the  print  statement  will  be
  412.  
  413. separated by the current output field separator when output.
  414.  
  415. Items not separated by commas will be concatenated, so
  416.  
  417.  
  418.    print $1 $2
  419.  
  420.  
  421. runs the first and second fields together.
  422.  
  423.  
  424.      The predefined variables NF and NR  can  be  used;  for
  425.  
  426. example
  427.  
  428.  
  429.    { print NR, NF, $0 }
  430.  
  431.  
  432. prints each record preceded by the  record  number  and  the
  433.  
  434. number of fields.
  435.  
  436.  
  437.      Output may be diverted to multiple files; the program
  438.  
  439.  
  440.    { print $1 >"foo1"; print $2 >"foo2" }
  441.  
  442.  
  443. writes the first field, $1, on the file foo1, and the second
  444.  
  445. field on file foo2.  The >> notation can also be used:
  446.  
  447.  
  448.    print $1 >>"foo"
  449.  
  450.  
  451. appends the output to the file foo.  (In each case, the out-
  452.  
  453. put  files are created if necessary.) The file name can be a
  454.  
  455. variable or a field as well as a constant; for example,
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                            - 6 -
  467.  
  468.  
  469.  
  470.    print $1 >$2
  471.  
  472.  
  473. uses the contents of field 2 as a file name.
  474.  
  475.  
  476.      Naturally there is a limit  on  the  number  of  output
  477.  
  478. files; currently it is 10.
  479.  
  480.  
  481.      Similarly, output can be piped into another process (on
  482.  
  483. UNIX only); for instance,
  484.  
  485.  
  486.    print | "mail bwk"
  487.  
  488.  
  489. mails the output to bwk.
  490.  
  491.  
  492.      The variables OFS and ORS may be  used  to  change  the
  493.  
  494. current  output field separator and output record separator.
  495.  
  496. The output record separator is appended to the output of the
  497.  
  498. print statement.
  499.  
  500.  
  501.      _A_w_k also provides the printf statement for output  for-
  502.  
  503. matting:
  504.  
  505.  
  506.    printf format expr, expr, ...
  507.  
  508.  
  509. formats the expressions in the list according to the specif-
  510.  
  511. ication in format and prints them.  For example,
  512.  
  513.  
  514.    printf "%8.2f  %10ld\n", $1, $2
  515.  
  516.  
  517. prints $1 as a floating point number 8 digits wide, with two
  518.  
  519. after  the  decimal point, and $2 as a 10-digit long decimal
  520.  
  521. number, followed by a newline.   No  output  separators  are
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                            - 7 -
  533.  
  534.  
  535. produced  automatically;  you  must add them yourself, as in
  536.  
  537. this example.  The version of printf is  identical  to  that
  538.  
  539. used with C.[2]
  540.  
  541.  
  542. _2.  _P_a_t_t_e_r_n_s
  543.  
  544.  
  545.      A pattern in front of an action acts as a selector that
  546.  
  547. determines  whether the action is to be executed.  A variety
  548.  
  549. of expressions may be used as patterns: regular expressions,
  550.  
  551. arithmetic  relational  expressions,  string-valued  expres-
  552.  
  553. sions, and arbitrary boolean combinations of these.
  554.  
  555.  
  556. _2._1.  _B_E_G_I_N _a_n_d _E_N_D
  557.  
  558.  
  559.      The special pattern BEGIN matches the beginning of  the
  560.  
  561. input,  before  the  first  record is read.  The pattern END
  562.  
  563. matches the end of the input, after the last record has been
  564.  
  565. processed.  BEGIN and END thus provide a way to gain control
  566.  
  567. before and after processing, for initialization and wrapup.
  568.  
  569.  
  570.      As an example, the field separator  can  be  set  to  a
  571.  
  572. colon by
  573.  
  574.  
  575.    BEGIN     { FS = ":" }
  576.  
  577.    ... _r_e_s_t _o_f _p_r_o_g_r_a_m ...
  578.  
  579.  
  580. Or the input lines may be counted by
  581.  
  582.  
  583.    END  { print NR }
  584.  
  585.  
  586. If BEGIN is present, it must be the first pattern; END  must
  587.  
  588. be the last if used.
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                            - 8 -
  599.  
  600.  
  601. _2._2.  _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n_s
  602.  
  603.  
  604.      The simplest regular expression is a literal string  of
  605.  
  606. characters enclosed in slashes, like
  607.  
  608.  
  609.    /smith/
  610.  
  611.  
  612. This is actually a complete _a_w_k program which will print all
  613.  
  614. lines  which  contain  any occurrence of the name ``smith''.
  615.  
  616. If a line contains ``smith'' as part of a  larger  word,  it
  617.  
  618. will also be printed, as in
  619.  
  620.  
  621.    blacksmithing
  622.  
  623.  
  624.  
  625.      _A_w_k regular expressions include the regular  expression
  626.  
  627. forms  found in the UNIX text editor _e_d[1] and _g_r_e_p (without
  628.  
  629. back-referencing).  In addition, _a_w_k allows parentheses  for
  630.  
  631. grouping,  |  for alternatives, + for ``one or more'', and ?
  632.  
  633. for ``zero or one'', all as in _l_e_x.  Character  classes  may
  634.  
  635. be  abbreviated:  [a-zA-Z0-9]  is the set of all letters and
  636.  
  637. digits.  As an example, the _a_w_k program
  638.  
  639.  
  640.    /[Aa]ho|[Ww]einberger|[Kk]ernighan/
  641.  
  642.  
  643. will print all lines which contain any of the names ``Aho,''
  644.  
  645. ``Weinberger'' or ``Kernighan,'' whether capitalized or not.
  646.  
  647.  
  648.      Regular expressions (with the extensions listed  above)
  649.  
  650. must  be enclosed in slashes, just as in _e_d and _s_e_d.  Within
  651.  
  652. a regular expression,  blanks  and  the  regular  expression
  653.  
  654. metacharacters  are  significant.   To  turn  of  the  magic
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                            - 9 -
  665.  
  666.  
  667. meaning of one of the regular expression characters, precede
  668.  
  669. it with a backslash.  An example is the pattern
  670.  
  671.  
  672.    /\/.*\//
  673.  
  674.  
  675. which matches any string of characters enclosed in slashes.
  676.  
  677.  
  678.      One can also specify that any field or variable matches
  679.  
  680. a  regular expression (or does not match it) with the opera-
  681.  
  682. tors ~ and !~.  The program
  683.  
  684.  
  685.    $1 ~ /[jJ]ohn/
  686.  
  687.  
  688. prints all lines where the first field matches  ``john''  or
  689.  
  690. ``John.''  Notice  that  this  will  also match ``Johnson'',
  691.  
  692. ``St. Johnsbury'', and so on.  To  restrict  it  to  exactly
  693.  
  694. [jJ]ohn, use
  695.  
  696.  
  697.    $1 ~ /^[jJ]ohn$/
  698.  
  699.  
  700. The caret ^ refers to the beginning of a line or field;  the
  701.  
  702. dollar sign $ refers to the end.
  703.  
  704.  
  705. _2._3.  _R_e_l_a_t_i_o_n_a_l _E_x_p_r_e_s_s_i_o_n_s
  706.  
  707.  
  708.      An _a_w_k pattern can be a relational expression involving
  709.  
  710. the usual relational operators <, <=, ==, !=, >=, and >.  An
  711.  
  712. example is
  713.  
  714.  
  715.    $2 > $1 + 100
  716.  
  717.  
  718. which selects lines where the second field is at  least  100
  719.  
  720. greater than the first field.  Similarly,
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                            - 10 -
  731.  
  732.  
  733.  
  734.    NF % 2 == 0
  735.  
  736.  
  737. prints lines with an even number of fields.
  738.  
  739.  
  740.      In relational tests, if neither operand is  numeric,  a
  741.  
  742. string comparison is made; otherwise it is numeric.  Thus,
  743.  
  744.  
  745.    $1 >= "s"
  746.  
  747.  
  748. selects lines that begin with an  s,  t,  u,  etc.   In  the
  749.  
  750. absence  of  any  other  information,  fields are treated as
  751.  
  752. strings, so the program
  753.  
  754.  
  755.    $1 > $2
  756.  
  757.  
  758. will perform a string comparison.
  759.  
  760.  
  761. _2._4.  _C_o_m_b_i_n_a_t_i_o_n_s _o_f _P_a_t_t_e_r_n_s
  762.  
  763.  
  764.      A pattern can be any boolean combination  of  patterns,
  765.  
  766. using  the  operators  ||  (or), && (and), and ! (not).  For
  767.  
  768. example,
  769.  
  770.  
  771.    $1 >= "s" && $1 < "t" && $1 != "smith"
  772.  
  773.  
  774. selects lines where the first field begins with  ``s'',  but
  775.  
  776. is  not  ``smith''.  && and || guarantee that their operands
  777.  
  778. will be evaluated from left to right;  evaluation  stops  as
  779.  
  780. soon as the truth or falsehood is determined.
  781.  
  782.  
  783. _2._5.  _P_a_t_t_e_r_n _R_a_n_g_e_s
  784.  
  785.  
  786.      The ``pattern'' that selects an action may also consist
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                            - 11 -
  797.  
  798.  
  799. of two patterns separated by a comma, as in
  800.  
  801.  
  802.    pat1, pat2     { ... }
  803.  
  804.  
  805. In this case, the action is performed for each line  between
  806.  
  807. an  occurrence  of  pat1  and  the  next  occurrence of pat2
  808.  
  809. (inclusive).  For example,
  810.  
  811.  
  812.    /start/, /stop/
  813.  
  814.  
  815. prints all lines between start and stop, while
  816.  
  817.  
  818.    NR == 100, NR == 200 { ... }
  819.  
  820.  
  821. does the action for lines 100 through 200 of the input.
  822.  
  823.  
  824. _3.  _A_c_t_i_o_n_s
  825.  
  826.  
  827.      An _a_w_k action is a sequence of action  statements  ter-
  828.  
  829. minated  by newlines or semicolons.  These action statements
  830.  
  831. can be used to do a variety of bookkeeping and string  mani-
  832.  
  833. pulating tasks.
  834.  
  835.  
  836. _3._1.  _B_u_i_l_t-_i_n _F_u_n_c_t_i_o_n_s
  837.  
  838.  
  839.      _A_w_k provides  a  ``length''  function  to  compute  the
  840.  
  841. length  of a string of characters.  This program prints each
  842.  
  843. record, preceded by its length:
  844.  
  845.  
  846.    {print length, $0}
  847.  
  848.  
  849. length by itself is a ``pseudo-variable'' which  yields  the
  850.  
  851. length of the current record; length(argument) is a function
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                            - 12 -
  863.  
  864.  
  865. which  yields  the  length  of  its  argument,  as  in   the
  866.  
  867. equivalent
  868.  
  869.  
  870.    {print length($0), $0}
  871.  
  872.  
  873. The argument may be any expression.
  874.  
  875.  
  876.      _A_w_k also provides the arithmetic functions  sqrt,  log,
  877.  
  878. exp,  and  int,  for square root, base _e logarithm, exponen-
  879.  
  880. tial, and integer part of their respective arguments.
  881.  
  882.  
  883.      The name of one of these  built-in  functions,  without
  884.  
  885. argument  or  parentheses, stands for the value of the func-
  886.  
  887. tion on the whole record.  The program
  888.  
  889.  
  890.    length < 10 || length > 20
  891.  
  892.  
  893. prints lines whose length is less than 10  or  greater  than
  894.  
  895. 20.
  896.  
  897.  
  898.      The function substr(s, m, n) produces the substring  of
  899.  
  900. s  that  begins  at  position  m (origin 1) and is at most n
  901.  
  902. characters long.  If n is omitted, the substring goes to the
  903.  
  904. end  of  s.  The function index(s1, s2) returns the position
  905.  
  906. where the string s2 occurs in s1, or zero if it does not.
  907.  
  908.  
  909.      The function sprintf(f, e1, e2, ...) produces the value
  910.  
  911. of the expressions e1, e2, etc., in the printf format speci-
  912.  
  913. fied by f.  Thus, for example,
  914.  
  915.  
  916.    x = sprintf("%8.2f %10ld", $1, $2)
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                            - 13 -
  929.  
  930.  
  931. sets x to the string produced by formatting the values of $1
  932.  
  933. and $2.
  934.  
  935.  
  936. _3._2.  _V_a_r_i_a_b_l_e_s, _E_x_p_r_e_s_s_i_o_n_s, _a_n_d _A_s_s_i_g_n_m_e_n_t_s
  937.  
  938.  
  939.      _A_w_k variables  take  on  numeric  (floating  point)  or
  940.  
  941. string values according to context.  For example, in
  942.  
  943.  
  944.    x = 1
  945.  
  946.  
  947. x is clearly a number, while in
  948.  
  949.  
  950.    x = "smith"
  951.  
  952.  
  953. it is clearly a string.  Strings are  converted  to  numbers
  954.  
  955. and vice versa whenever context demands it.  For instance,
  956.  
  957.  
  958.    x = "3" + "4"
  959.  
  960.  
  961. assigns 7 to x.  Strings  which  cannot  be  interpreted  as
  962.  
  963. numbers  in  a numerical context will generally have numeric
  964.  
  965. value zero, but it is unwise to count on this behavior.
  966.  
  967.  
  968.      By default, variables (other than built-ins)  are  ini-
  969.  
  970. tialized to the null string, which has numerical value zero;
  971.  
  972. this eliminates the need for most BEGIN sections.  For exam-
  973.  
  974. ple, the sums of the first two fields can be computed by
  975.  
  976.  
  977.         { s1 += $1; s2 += $2 }
  978.  
  979.    END  { print s1, s2 }
  980.  
  981.  
  982.  
  983.      Arithmetic is done internally in floating  point.   The
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                            - 14 -
  995.  
  996.  
  997. arithmetic  operators  are  +,  -, *, /, and % (mod).  The C
  998.  
  999. increment ++ and decrement -- operators are also  available,
  1000.  
  1001. and  so are the assignment operators +=, -=, *=, /=, and %=.
  1002.  
  1003. These operators may all be used in expressions.
  1004.  
  1005.  
  1006. _3._3.  _F_i_e_l_d _V_a_r_i_a_b_l_e_s
  1007.  
  1008.  
  1009.      Fields in _a_w_k share essentially all of  the  properties
  1010.  
  1011. of  variables  -  they  may  be used in arithmetic or string
  1012.  
  1013. operations, and may be assigned to.  Thus  one  can  replace
  1014.  
  1015. the first field with a sequence number like this:
  1016.  
  1017.  
  1018.    { $1 = NR; print }
  1019.  
  1020.  
  1021. or accumulate two fields into a third, like this:
  1022.  
  1023.  
  1024.    { $1 = $2 + $3; print $0 }
  1025.  
  1026.  
  1027. or assign a string to a field:
  1028.  
  1029.  
  1030.    { if ($3 > 1000)
  1031.  
  1032.         $3 = "too big"
  1033.  
  1034.      print
  1035.  
  1036.    }
  1037.  
  1038.  
  1039. which replaces the third field by ``too big''  when  it  is,
  1040.  
  1041. and in any case prints the record.
  1042.  
  1043.  
  1044.      Field references may be numerical expressions, as in
  1045.  
  1046.  
  1047.    { print $i, $(i+1), $(i+n) }
  1048.  
  1049.  
  1050. Whether a field is  deemed  numeric  or  string  depends  on
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                            - 15 -
  1061.  
  1062.  
  1063. context; in ambiguous cases like
  1064.  
  1065.  
  1066.    if ($1 == $2) ...
  1067.  
  1068.  
  1069. fields are treated as strings.
  1070.  
  1071.  
  1072.      Each input line is split into fields  automatically  as
  1073.  
  1074. necessary.   It  is  also  possible to split any variable or
  1075.  
  1076. string into fields:
  1077.  
  1078.  
  1079.    n = split(s, array, sep)
  1080.  
  1081.  
  1082. splits the the string s into array[1], ...,  array[n].   The
  1083.  
  1084. number  of  elements found is returned.  If the sep argument
  1085.  
  1086. is provided, it is used as the field separator; otherwise FS
  1087.  
  1088. is used as the separator.
  1089.  
  1090.  
  1091. _3._4.  _S_t_r_i_n_g _C_o_n_c_a_t_e_n_a_t_i_o_n
  1092.  
  1093.  
  1094.      Strings may be concatenated.  For example
  1095.  
  1096.  
  1097.    length($1 $2 $3)
  1098.  
  1099.  
  1100. returns the length of the first three fields.  Or in a print
  1101.  
  1102. statement,
  1103.  
  1104.  
  1105.    print $1 " is " $2
  1106.  
  1107.  
  1108. prints the two fields separated by `` is ''.  Variables  and
  1109.  
  1110. numeric expressions may also appear in concatenations.
  1111.  
  1112.  
  1113. _3._5.  _A_r_r_a_y_s
  1114.  
  1115.  
  1116.      Array elements  are  not  declared;  they  spring  into
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                            - 16 -
  1127.  
  1128.  
  1129. existence  by being mentioned.  Subscripts may have _a_n_y non-
  1130.  
  1131. null value, including non-numeric strings.  As an example of
  1132.  
  1133. a conventional numeric subscript, the statement
  1134.  
  1135.  
  1136.    x[NR] = $0
  1137.  
  1138.  
  1139. assigns the current input record to the NR-_t_h element of the
  1140.  
  1141. array  x.   In  fact,  it  is  possible in principle (though
  1142.  
  1143. perhaps slow) to process the entire input in a random  order
  1144.  
  1145. with the _a_w_k program
  1146.  
  1147.  
  1148.         { x[NR] = $0 }
  1149.  
  1150.    END  { ... _p_r_o_g_r_a_m ... }
  1151.  
  1152.  
  1153. The first action merely records each input line in the array
  1154.  
  1155. x.
  1156.  
  1157.  
  1158.      Array elements may  be  named  by  non-numeric  values,
  1159.  
  1160. which  gives  _a_w_k  a  capability rather like the associative
  1161.  
  1162. memory of Snobol tables.  Suppose the input contains  fields
  1163.  
  1164. with values like apple, orange, etc.  Then the program
  1165.  
  1166.  
  1167.    /apple/   { x["apple"]++ }
  1168.  
  1169.    /orange/  { x["orange"]++ }
  1170.  
  1171.    END       { print x["apple"], x["orange"] }
  1172.  
  1173.  
  1174. increments counts for the named array elements,  and  prints
  1175.  
  1176. them at the end of the input.
  1177.  
  1178.  
  1179. _3._6.  _F_l_o_w-_o_f-_C_o_n_t_r_o_l _S_t_a_t_e_m_e_n_t_s
  1180.  
  1181.  
  1182.      _A_w_k provides the basic flow-of-control  statements  if-
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                            - 17 -
  1193.  
  1194.  
  1195. else,  while, for, and statement grouping with braces, as in
  1196.  
  1197. C.  We showed  the  if  statement  in  section  3.3  without
  1198.  
  1199. describing  it.   The condition in parentheses is evaluated;
  1200.  
  1201. if it is true, the statement following the if is done.   The
  1202.  
  1203. else part is optional.
  1204.  
  1205.  
  1206.      The while statement is exactly like  that  of  C.   For
  1207.  
  1208. example, to print all input fields one per line,
  1209.  
  1210.  
  1211.    i = 1
  1212.  
  1213.    while (i <= NF) {
  1214.  
  1215.         print $i
  1216.  
  1217.         ++i
  1218.  
  1219.    }
  1220.  
  1221.  
  1222.  
  1223.      The for statement is also exactly that of C:
  1224.  
  1225.  
  1226.    for (i = 1; i <= NF; i++)
  1227.  
  1228.         print $i
  1229.  
  1230.  
  1231. does the same job as the while statement above.
  1232.  
  1233.  
  1234.      There is an alternate form of the for  statement  which
  1235.  
  1236. is  suited  for  accessing  the  elements  of an associative
  1237.  
  1238. array:
  1239.  
  1240.  
  1241.    for (i in array)
  1242.  
  1243.         _s_t_a_t_e_m_e_n_t
  1244.  
  1245.  
  1246. does _s_t_a_t_e_m_e_n_t with i set in turn to each element of  array.
  1247.  
  1248. The  elements  are  accessed  in an apparently random order.
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                            - 18 -
  1259.  
  1260.  
  1261. Chaos will ensue if i is altered, or if any new elements are
  1262.  
  1263. accessed during the loop.
  1264.  
  1265.  
  1266.      The expression in the condition part of an if, while or
  1267.  
  1268. for  can  include relational operators like <, <=, >, >=, ==
  1269.  
  1270. (``is equal  to''),  and  !=  (``not  equal  to'');  regular
  1271.  
  1272. expression  matches  with  the match operators ~ and !~; the
  1273.  
  1274. logical operators ||, &&, and !; and of  course  parentheses
  1275.  
  1276. for grouping.
  1277.  
  1278.  
  1279.      The break statement causes an immediate  exit  from  an
  1280.  
  1281. enclosing  while  or  for; the continue statement causes the
  1282.  
  1283. next iteration to begin.
  1284.  
  1285.  
  1286.      The statement next causes _a_w_k to  skip  immediately  to
  1287.  
  1288. the  next  record  and  begin scanning the patterns from the
  1289.  
  1290. top.  The statement exit causes the program to behave as  if
  1291.  
  1292. the end of the input had occurred.
  1293.  
  1294.  
  1295.      Comments may be placed in _a_w_k programs: they begin with
  1296.  
  1297. the character # and end with the end of the line, as in
  1298.  
  1299.  
  1300.    print x, y     # this is a comment
  1301.  
  1302.  
  1303.  
  1304. _4.  _D_e_s_i_g_n
  1305.  
  1306.  
  1307.      The UNIX system already provides several programs  that
  1308.  
  1309. operate  by  passing  input  through  a selection mechanism.
  1310.  
  1311. _G_r_e_p, the first and simplest, merely prints all lines  which
  1312.  
  1313. match  a single specified pattern.  _E_g_r_e_p provides more gen-
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                            - 19 -
  1325.  
  1326.  
  1327. eral patterns, i.e., regular expressions in full generality;
  1328.  
  1329. _f_g_r_e_p  searches  for  a  set of keywords with a particularly
  1330.  
  1331. fast algorithm.  _S_e_d[1] provides most of the editing facili-
  1332.  
  1333. ties  of  the editor _e_d, applied to a stream of input.  None
  1334.  
  1335. of these programs  provides  numeric  capabilities,  logical
  1336.  
  1337. relations, or variables.
  1338.  
  1339.  
  1340.      _L_e_x[3] provides general regular expression  recognition
  1341.  
  1342. capabilities,  and,  by serving as a C program generator, is
  1343.  
  1344. essentially open-ended in its capabilities.  The use of _l_e_x,
  1345.  
  1346. however,  requires  a  knowledge of C programming, and a _l_e_x
  1347.  
  1348. program must  be  compiled  and  loaded  before  use,  which
  1349.  
  1350. discourages its use for one-shot applications.
  1351.  
  1352.  
  1353.      _A_w_k is an attempt to fill in another part of the matrix
  1354.  
  1355. of  possibilities.   It  provides general regular expression
  1356.  
  1357. capabilities and an implicit input/output loop.  But it also
  1358.  
  1359. provides convenient numeric processing, variables, more gen-
  1360.  
  1361. eral selection, and control flow in the  actions.   It  does
  1362.  
  1363. not  require  compilation or a knowledge of C.  Finally, _a_w_k
  1364.  
  1365. provides a convenient way to access fields within lines;  it
  1366.  
  1367. is unique in this respect.
  1368.  
  1369.  
  1370.      _A_w_k also tries to integrate strings  and  numbers  com-
  1371.  
  1372. pletely,  by  treating  all  quantities  as  both string and
  1373.  
  1374. numeric, deciding which  representation  is  appropriate  as
  1375.  
  1376. late  as possible.  In most cases the user can simply ignore
  1377.  
  1378. the differences.
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.                            - 20 -
  1391.  
  1392.  
  1393.      Most of the effort in developing _a_w_k went into deciding
  1394.  
  1395. what  _a_w_k  should or should not do (for instance, it doesn't
  1396.  
  1397. do string substitution) and what the syntax  should  be  (no
  1398.  
  1399. explicit  operator for concatenation) rather than on writing
  1400.  
  1401. or debugging the code.  We have tried  to  make  the  syntax
  1402.  
  1403. powerful but easy to use and well adapted to scanning files.
  1404.  
  1405. For example, the absence of declarations and  implicit  ini-
  1406.  
  1407. tializations,  while  probably  a  bad  idea  for a general-
  1408.  
  1409. purpose programming language, is  desirable  in  a  language
  1410.  
  1411. that  is meant to be used for tiny programs that may even be
  1412.  
  1413. composed on the command line.
  1414.  
  1415.  
  1416.      In practice, _a_w_k usage seems to  fall  into  two  broad
  1417.  
  1418. categories.   One  is  what might be called ``report genera-
  1419.  
  1420. tion'' - processing an input to extract counts,  sums,  sub-
  1421.  
  1422. totals, etc.  This also includes the writing of trivial data
  1423.  
  1424. validation programs, such as verifying that a field contains
  1425.  
  1426. only  numeric  information  or  that  certain delimiters are
  1427.  
  1428. properly balanced.  The combination of textual  and  numeric
  1429.  
  1430. processing is invaluable here.
  1431.  
  1432.  
  1433.      A second area of use is as a data transformer, convert-
  1434.  
  1435. ing  data  from  the  form produced by one program into that
  1436.  
  1437. expected by another.  The simplest  examples  merely  select
  1438.  
  1439. fields, perhaps with rearrangements.
  1440.  
  1441.  
  1442. _5.  _I_m_p_l_e_m_e_n_t_a_t_i_o_n
  1443.  
  1444.  
  1445.      The actual implementation  of  _a_w_k  uses  the  language
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                            - 21 -
  1457.  
  1458.  
  1459. development  tools  available  on the UNIX operating system.
  1460.  
  1461. The grammar is specified with _y_a_c_c;[4] the lexical  analysis
  1462.  
  1463. is  done  by  _l_e_x;  the  regular  expression recognizers are
  1464.  
  1465. deterministic finite automata constructed directly from  the
  1466.  
  1467. expressions.  An _a_w_k program is translated into a parse tree
  1468.  
  1469. which is then directly executed by a simple interpreter.
  1470.  
  1471.  
  1472.      _A_w_k was designed for ease of use rather than processing
  1473.  
  1474. speed;  the  delayed  evaluation  of  variable types and the
  1475.  
  1476. necessity to break input into fields makes high speed diffi-
  1477.  
  1478. cult  to  achieve in any case.  Nonetheless, the program has
  1479.  
  1480. not proven to be unworkably slow.
  1481.  
  1482.  
  1483.      Table I below shows the execution (user + system)  time
  1484.  
  1485. on  a PDP-11/70 of the UNIX programs _w_c, _g_r_e_p, _e_g_r_e_p, _f_g_r_e_p,
  1486.  
  1487. _s_e_d, _l_e_x, and _a_w_k on the following simple tasks:
  1488.  
  1489.  
  1490.   1. count the number of lines.
  1491.  
  1492.  
  1493.   2. print all lines containing ``doug''.
  1494.  
  1495.  
  1496.   3. print  all  lines  containing  ``doug'',   ``ken''   or
  1497.  
  1498.      ``dmr''.
  1499.  
  1500.  
  1501.   4. print the third field of each line.
  1502.  
  1503.  
  1504.   5. print the third and second fields of each line, in that
  1505.  
  1506.      order.
  1507.  
  1508.  
  1509.   6. append all  lines  containing  ``doug'',  ``ken'',  and
  1510.  
  1511.      ``dmr''  to  files  ``jdoug'',  ``jken'', and ``jdmr'',
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.                            - 22 -
  1523.  
  1524.  
  1525.      respectively.
  1526.  
  1527.  
  1528.   7. print each line prefixed by ``line-number : ''.
  1529.  
  1530.  
  1531.   8. sum the fourth column of a table.
  1532.  
  1533.  
  1534. The program _w_c merely counts words, lines and characters  in
  1535.  
  1536. its  input;  we  have  already mentioned the others.  In all
  1537.  
  1538. cases the input  was  a  file  containing  10,000  lines  as
  1539.  
  1540. created by the command _l_s -_l; each line has the form
  1541.  
  1542.  
  1543.    -rw-rw-rw- 1 ava 123 Oct 15 17:05 xxx
  1544.  
  1545.  
  1546. The total length of this input is 452,960 characters.  Times
  1547.  
  1548. for _l_e_x do not include compile or load.
  1549.  
  1550.  
  1551.      As might be expected, _a_w_k is not as fast  as  the  spe-
  1552.  
  1553. cialized  tools _w_c, _s_e_d, or the programs in the _g_r_e_p family,
  1554.  
  1555. but is faster than the more general tool _l_e_x.  In all cases,
  1556.  
  1557. the  tasks  were about as easy to express as _a_w_k programs as
  1558.  
  1559. programs in these other languages;  tasks  involving  fields
  1560.  
  1561. were  considerably  easier to express as _a_w_k programs.  Some
  1562.  
  1563. of the test programs are shown in _a_w_k, _s_e_d and _l_e_x.
  1564.  
  1565.  
  1566. _R_e_f_e_r_e_n_c_e_s
  1567.  
  1568.  
  1569.  
  1570. 1.   K.  Thompson  and  D.  M.  Ritchie,  _U_N_I_X  _P_r_o_g_r_a_m_m_e_r'_s
  1571.  
  1572.      _M_a_n_u_a_l, Bell Laboratories, May 1975.  Sixth Edition
  1573.  
  1574.  
  1575. 2.   B. W. Kernighan and D. M. Ritchie,  _T_h_e  _C  _P_r_o_g_r_a_m_m_i_n_g
  1576.  
  1577.      _L_a_n_g_u_a_g_e,  Prentice-Hall, Englewood Cliffs, New Jersey,
  1578.  
  1579.  
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.                            - 23 -
  1589.  
  1590.  
  1591.      1978.
  1592.  
  1593.  
  1594. 3.   M. E. Lesk, "Lex - A Lexical Analyzer Generator," Comp.
  1595.  
  1596.      Sci. Tech. Rep. No. 39, Bell Laboratories, Murray Hill,
  1597.  
  1598.      New Jersey, October 1975.
  1599.  
  1600.  
  1601. 4.   S. C. Johnson, "Yacc -  Yet Another Compiler-Compiler,"
  1602.  
  1603.      Comp. Sci. Tech. Rep. No. 32, Bell Laboratories, Murray
  1604.  
  1605.      Hill, New Jersey, July 1975.
  1606.  
  1607.  
  1608.                                  Task
  1609.  
  1610. Program    1       2       3      4      5       6      7      8
  1611.  
  1612. ___________________________________________________________________
  1613.   _w_c   |   8.6|       |       |      |      |       |      |      |
  1614.        |      |       |       |      |      |       |      |      |
  1615.  _g_r_e_p  |  11.7|   13.1|       |      |      |       |      |      |
  1616.        |      |       |       |      |      |       |      |      |
  1617.  _e_g_r_e_p |   6.2|   11.5|   11.6|      |      |       |      |      |
  1618.        |      |       |       |      |      |       |      |      |
  1619.  _f_g_r_e_p |   7.7|   13.8|   16.1|      |      |       |      |      |
  1620.        |      |       |       |      |      |       |      |      |
  1621.   _s_e_d  |  10.2|   11.6|   15.8|  29.0|  30.5|   16.1|      |      |
  1622.        |      |       |       |      |      |       |      |      |
  1623.   _l_e_x  |  65.1|  150.1|  144.2|  67.7|  70.3|  104.0|  81.7|  92.8|
  1624.        |      |       |       |      |      |       |      |      |
  1625.   _a_w_k  |  15.0|   25.6|   29.9|  33.3|  38.9|   46.4|  71.4|  31.1|
  1626.        |      |       |       |      |      |       |      |      |
  1627. ________|_______|________|________|_______|_______|________|_______|_______|
  1628.  
  1629.  
  1630.      Table I.  Execution Times of Programs. (Times are in sec.)
  1631.  
  1632.  
  1633.  
  1634.      The programs  for  some
  1635.                                    1.   END  {print NR}
  1636. of   these  jobs  are  shown
  1637.  
  1638. below.  The _l_e_x programs are
  1639.                                    2.   /doug/
  1640. generally too long to show.
  1641.  
  1642.  
  1643. AWK:                               3.   /ken|doug|dmr/
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.                            - 24 -
  1655.  
  1656.  
  1657.  
  1658.    4.   {print $3}                 4.   /[^ ]* [ ]*[^ ]* [ ]*\([^ ]*\) .*/s//\1/p
  1659.  
  1660.  
  1661.  
  1662.    5.   {print $3, $2}             5.   /[^ ]* [ ]*\([^ ]*\) [ ]*\([^ ]*\) .*/s//\2 \1/p
  1663.  
  1664.  
  1665.  
  1666.    6.   /ken/     {print >"jken"}  6.   /ken/w jken
  1667.  
  1668.         /doug/    {print >"jdoug"}      /doug/w jdoug
  1669.  
  1670.         /dmr/     {print >"jdmr"}       /dmr/w jdmr
  1671.  
  1672.  
  1673.  
  1674.    7.   {print NR ": " $0}      LEX:
  1675.  
  1676.  
  1677.  
  1678.    8.        {sum = sum + $4}      1.   %{
  1679.  
  1680.         END  {print sum}                int i;
  1681.  
  1682.                                         %}
  1683.  
  1684. SED:                                    %%
  1685.  
  1686.                                         \n   i++;
  1687.  
  1688.    1.   $=                              .    ;
  1689.  
  1690.                                         %%
  1691.  
  1692.    2.   /doug/p                         yywrap() {
  1693.  
  1694.                                              printf("%d\n", i);
  1695.  
  1696.    3.   /doug/p                         }
  1697.  
  1698.         /doug/d
  1699.  
  1700.         /ken/p                     2.   %%
  1701.  
  1702.         /ken/d                          ^.*doug.*$     printf("%s\n", yytext);
  1703.  
  1704.         /dmr/p                          .    ;
  1705.  
  1706.         /dmr/d                          \n   ;
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.