home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / info / gawk.info-8 < prev    next >
Encoding:
GNU Info File  |  1996-10-12  |  39.9 KB  |  1,174 lines

  1. This is Info file gawk.info, produced by Makeinfo-1.55 from the input
  2. file /gnu-src/gawk-2.15.6/gawk.texi.
  3.  
  4.    This file documents `awk', a program that you can use to select
  5. particular records in a file and perform operations upon them.
  6.  
  7.    This is Edition 0.15 of `The GAWK Manual',
  8. for the 2.15 version of the GNU implementation
  9. of AWK.
  10.  
  11.    Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  12.  
  13.    Permission is granted to make and distribute verbatim copies of this
  14. manual provided the copyright notice and this permission notice are
  15. preserved on all copies.
  16.  
  17.    Permission is granted to copy and distribute modified versions of
  18. this manual under the conditions for verbatim copying, provided that
  19. the entire resulting derived work is distributed under the terms of a
  20. permission notice identical to this one.
  21.  
  22.    Permission is granted to copy and distribute translations of this
  23. manual into another language, under the above conditions for modified
  24. versions, except that this permission notice may be stated in a
  25. translation approved by the Foundation.
  26.  
  27. 
  28. File: gawk.info,  Node: Regexp Summary,  Next: Actions Summary,  Prev: Pattern Summary,  Up: Rules Summary
  29.  
  30. Regular Expressions
  31. -------------------
  32.  
  33.    Regular expressions are the extended kind found in `egrep'.  They
  34. are composed of characters as follows:
  35.  
  36. `C'
  37.      matches the character C (assuming C is a character with no special
  38.      meaning in regexps).
  39.  
  40. `\C'
  41.      matches the literal character C.
  42.  
  43. `.'
  44.      matches any character except newline.
  45.  
  46. `^'
  47.      matches the beginning of a line or a string.
  48.  
  49. `$'
  50.      matches the end of a line or a string.
  51.  
  52. `[ABC...]'
  53.      matches any of the characters ABC... (character class).
  54.  
  55. `[^ABC...]'
  56.      matches any character except ABC... and newline (negated character
  57.      class).
  58.  
  59. `R1|R2'
  60.      matches either R1 or R2 (alternation).
  61.  
  62. `R1R2'
  63.      matches R1, and then R2 (concatenation).
  64.  
  65. `R+'
  66.      matches one or more R's.
  67.  
  68. `R*'
  69.      matches zero or more R's.
  70.  
  71. `R?'
  72.      matches zero or one R's.
  73.  
  74. `(R)'
  75.      matches R (grouping).
  76.  
  77.    *Note Regular Expressions as Patterns: Regexp, for a more detailed
  78. explanation of regular expressions.
  79.  
  80.    The escape sequences allowed in string constants are also valid in
  81. regular expressions (*note Constant Expressions: Constants.).
  82.  
  83. 
  84. File: gawk.info,  Node: Actions Summary,  Prev: Regexp Summary,  Up: Rules Summary
  85.  
  86. Actions
  87. -------
  88.  
  89.    Action statements are enclosed in braces, `{' and `}'.  Action
  90. statements consist of the usual assignment, conditional, and looping
  91. statements found in most languages.  The operators, control statements,
  92. and input/output statements available are patterned after those in C.
  93.  
  94. * Menu:
  95.  
  96. * Operator Summary::            `awk' operators.
  97. * Control Flow Summary::        The control statements.
  98. * I/O Summary::                 The I/O statements.
  99. * Printf Summary::              A summary of `printf'.
  100. * Special File Summary::        Special file names interpreted internally.
  101. * Numeric Functions Summary::   Built-in numeric functions.
  102. * String Functions Summary::    Built-in string functions.
  103. * Time Functions Summary::      Built-in time functions.
  104. * String Constants Summary::    Escape sequences in strings.
  105.  
  106. 
  107. File: gawk.info,  Node: Operator Summary,  Next: Control Flow Summary,  Prev: Actions Summary,  Up: Actions Summary
  108.  
  109. Operators
  110. .........
  111.  
  112.    The operators in `awk', in order of increasing precedence, are:
  113.  
  114. `= += -= *= /= %= ^='
  115.      Assignment.  Both absolute assignment (`VAR=VALUE') and operator
  116.      assignment (the other forms) are supported.
  117.  
  118. `?:'
  119.      A conditional expression, as in C.  This has the form `EXPR1 ?
  120.      eXPR2 : EXPR3'.  If EXPR1 is true, the value of the expression is
  121.      EXPR2; otherwise it is EXPR3.  Only one of EXPR2 and EXPR3 is
  122.      evaluated.
  123.  
  124. `||'
  125.      Logical "or".
  126.  
  127. `&&'
  128.      Logical "and".
  129.  
  130. `~ !~'
  131.      Regular expression match, negated match.
  132.  
  133. `< <= > >= != =='
  134.      The usual relational operators.
  135.  
  136. `BLANK'
  137.      String concatenation.
  138.  
  139. `+ -'
  140.      Addition and subtraction.
  141.  
  142. `* / %'
  143.      Multiplication, division, and modulus.
  144.  
  145. `+ - !'
  146.      Unary plus, unary minus, and logical negation.
  147.  
  148. `^'
  149.      Exponentiation (`**' may also be used, and `**=' for the assignment
  150.      operator, but they are not specified in the POSIX standard).
  151.  
  152. `++ --'
  153.      Increment and decrement, both prefix and postfix.
  154.  
  155. `$'
  156.      Field reference.
  157.  
  158.    *Note Expressions as Action Statements: Expressions, for a full
  159. description of all the operators listed above.  *Note Examining Fields:
  160. Fields, for a description of the field reference operator.
  161.  
  162. 
  163. File: gawk.info,  Node: Control Flow Summary,  Next: I/O Summary,  Prev: Operator Summary,  Up: Actions Summary
  164.  
  165. Control Statements
  166. ..................
  167.  
  168.    The control statements are as follows:
  169.  
  170.      if (CONDITION) STATEMENT [ else STATEMENT ]
  171.      while (CONDITION) STATEMENT
  172.      do STATEMENT while (CONDITION)
  173.      for (EXPR1; EXPR2; EXPR3) STATEMENT
  174.      for (VAR in ARRAY) STATEMENT
  175.      break
  176.      continue
  177.      delete ARRAY[INDEX]
  178.      exit [ EXPRESSION ]
  179.      { STATEMENTS }
  180.  
  181.    *Note Control Statements in Actions: Statements, for a full
  182. description of all the control statements listed above.
  183.  
  184. 
  185. File: gawk.info,  Node: I/O Summary,  Next: Printf Summary,  Prev: Control Flow Summary,  Up: Actions Summary
  186.  
  187. I/O Statements
  188. ..............
  189.  
  190.    The input/output statements are as follows:
  191.  
  192. `getline'
  193.      Set `$0' from next input record; set `NF', `NR', `FNR'.
  194.  
  195. `getline <FILE'
  196.      Set `$0' from next record of FILE; set `NF'.
  197.  
  198. `getline VAR'
  199.      Set VAR from next input record; set `NF', `FNR'.
  200.  
  201. `getline VAR <FILE'
  202.      Set VAR from next record of FILE.
  203.  
  204. `next'
  205.      Stop processing the current input record.  The next input record
  206.      is read and processing starts over with the first pattern in the
  207.      `awk' program.  If the end of the input data is reached, the `END'
  208.      rule(s), if any, are executed.
  209.  
  210. `next file'
  211.      Stop processing the current input file.  The next input record
  212.      read comes from the next input file.  `FILENAME' is updated, `FNR'
  213.      is set to 1, and processing starts over with the first pattern in
  214.      the `awk' program.  If the end of the input data is reached, the
  215.      `END' rule(s), if any, are executed.
  216.  
  217. `print'
  218.      Prints the current record.
  219.  
  220. `print EXPR-LIST'
  221.      Prints expressions.
  222.  
  223. `print EXPR-LIST > FILE'
  224.      Prints expressions on FILE.
  225.  
  226. `printf FMT, EXPR-LIST'
  227.      Format and print.
  228.  
  229. `printf FMT, EXPR-LIST > file'
  230.      Format and print on FILE.
  231.  
  232.    Other input/output redirections are also allowed.  For `print' and
  233. `printf', `>> FILE' appends output to the FILE, and `| COMMAND' writes
  234. on a pipe.  In a similar fashion, `COMMAND | getline' pipes input into
  235. `getline'.  `getline' returns 0 on end of file, and -1 on an error.
  236.  
  237.    *Note Explicit Input with `getline': Getline, for a full description
  238. of the `getline' statement.  *Note Printing Output: Printing, for a
  239. full description of `print' and `printf'.  Finally, *note The `next'
  240. Statement: Next Statement., for a description of how the `next'
  241. statement works.
  242.  
  243. 
  244. File: gawk.info,  Node: Printf Summary,  Next: Special File Summary,  Prev: I/O Summary,  Up: Actions Summary
  245.  
  246. `printf' Summary
  247. ................
  248.  
  249.    The `awk' `printf' statement and `sprintf' function accept the
  250. following conversion specification formats:
  251.  
  252. `%c'
  253.      An ASCII character.  If the argument used for `%c' is numeric, it
  254.      is treated as a character and printed.  Otherwise, the argument is
  255.      assumed to be a string, and the only first character of that
  256.      string is printed.
  257.  
  258. `%d'
  259. `%i'
  260.      A decimal number (the integer part).
  261.  
  262. `%e'
  263.      A floating point number of the form `[-]d.ddddddE[+-]dd'.
  264.  
  265. `%f'
  266.      A floating point number of the form [`-']`ddd.dddddd'.
  267.  
  268. `%g'
  269.      Use `%e' or `%f' conversion, whichever produces a shorter string,
  270.      with nonsignificant zeros suppressed.
  271.  
  272. `%o'
  273.      An unsigned octal number (again, an integer).
  274.  
  275. `%s'
  276.      A character string.
  277.  
  278. `%x'
  279.      An unsigned hexadecimal number (an integer).
  280.  
  281. `%X'
  282.      Like `%x', except use `A' through `F' instead of `a' through `f'
  283.      for decimal 10 through 15.
  284.  
  285. `%%'
  286.      A single `%' character; no argument is converted.
  287.  
  288.    There are optional, additional parameters that may lie between the
  289. `%' and the control letter:
  290.  
  291. `-'
  292.      The expression should be left-justified within its field.
  293.  
  294. `WIDTH'
  295.      The field should be padded to this width.  If WIDTH has a leading
  296.      zero, then the field is padded with zeros.  Otherwise it is padded
  297.      with blanks.
  298.  
  299. `.PREC'
  300.      A number indicating the maximum width of strings or digits to the
  301.      right of the decimal point.
  302.  
  303.    Either or both of the WIDTH and PREC values may be specified as `*'.
  304. In that case, the particular value is taken from the argument list.
  305.  
  306.    *Note Using `printf' Statements for Fancier Printing: Printf, for
  307. examples and for a more detailed description.
  308.  
  309. 
  310. File: gawk.info,  Node: Special File Summary,  Next: Numeric Functions Summary,  Prev: Printf Summary,  Up: Actions Summary
  311.  
  312. Special File Names
  313. ..................
  314.  
  315.    When doing I/O redirection from either `print' or `printf' into a
  316. file, or via `getline' from a file, `gawk' recognizes certain special
  317. file names internally.  These file names allow access to open file
  318. descriptors inherited from `gawk''s parent process (usually the shell).
  319. The file names are:
  320.  
  321. `/dev/stdin'
  322.      The standard input.
  323.  
  324. `/dev/stdout'
  325.      The standard output.
  326.  
  327. `/dev/stderr'
  328.      The standard error output.
  329.  
  330. `/dev/fd/N'
  331.      The file denoted by the open file descriptor N.
  332.  
  333.    In addition the following files provide process related information
  334. about the running `gawk' program.
  335.  
  336. `/dev/pid'
  337.      Reading this file returns the process ID of the current process,
  338.      in decimal, terminated with a newline.
  339.  
  340. `/dev/ppid'
  341.      Reading this file returns the parent process ID of the current
  342.      process, in decimal, terminated with a newline.
  343.  
  344. `/dev/pgrpid'
  345.      Reading this file returns the process group ID of the current
  346.      process, in decimal, terminated with a newline.
  347.  
  348. `/dev/user'
  349.      Reading this file returns a single record terminated with a
  350.      newline.  The fields are separated with blanks.  The fields
  351.      represent the following information:
  352.  
  353.     `$1'
  354.           The value of the `getuid' system call.
  355.  
  356.     `$2'
  357.           The value of the `geteuid' system call.
  358.  
  359.     `$3'
  360.           The value of the `getgid' system call.
  361.  
  362.     `$4'
  363.           The value of the `getegid' system call.
  364.  
  365.      If there are any additional fields, they are the group IDs
  366.      returned by `getgroups' system call.  (Multiple groups may not be
  367.      supported on all systems.)
  368.  
  369. These file names may also be used on the command line to name data
  370. files.  These file names are only recognized internally if you do not
  371. actually have files by these names on your system.
  372.  
  373.    *Note Standard I/O Streams: Special Files, for a longer description
  374. that provides the motivation for this feature.
  375.  
  376. 
  377. File: gawk.info,  Node: Numeric Functions Summary,  Next: String Functions Summary,  Prev: Special File Summary,  Up: Actions Summary
  378.  
  379. Numeric Functions
  380. .................
  381.  
  382.    `awk' has the following predefined arithmetic functions:
  383.  
  384. `atan2(Y, X)'
  385.      returns the arctangent of Y/X in radians.
  386.  
  387. `cos(EXPR)'
  388.      returns the cosine in radians.
  389.  
  390. `exp(EXPR)'
  391.      the exponential function.
  392.  
  393. `int(EXPR)'
  394.      truncates to integer.
  395.  
  396. `log(EXPR)'
  397.      the natural logarithm function.
  398.  
  399. `rand()'
  400.      returns a random number between 0 and 1.
  401.  
  402. `sin(EXPR)'
  403.      returns the sine in radians.
  404.  
  405. `sqrt(EXPR)'
  406.      the square root function.
  407.  
  408. `srand(EXPR)'
  409.      use EXPR as a new seed for the random number generator.  If no EXPR
  410.      is provided, the time of day is used.  The return value is the
  411.      previous seed for the random number generator.
  412.  
  413. 
  414. File: gawk.info,  Node: String Functions Summary,  Next: Time Functions Summary,  Prev: Numeric Functions Summary,  Up: Actions Summary
  415.  
  416. String Functions
  417. ................
  418.  
  419.    `awk' has the following predefined string functions:
  420.  
  421. `gsub(R, S, T)'
  422.      for each substring matching the regular expression R in the string
  423.      T, substitute the string S, and return the number of substitutions.
  424.      If T is not supplied, use `$0'.
  425.  
  426. `index(S, T)'
  427.      returns the index of the string T in the string S, or 0 if T is
  428.      not present.
  429.  
  430. `length(S)'
  431.      returns the length of the string S.  The length of `$0' is
  432.      returned if no argument is supplied.
  433.  
  434. `match(S, R)'
  435.      returns the position in S where the regular expression R occurs,
  436.      or 0 if R is not present, and sets the values of `RSTART' and
  437.      `RLENGTH'.
  438.  
  439. `split(S, A, R)'
  440.      splits the string S into the array A on the regular expression R,
  441.      and returns the number of fields.  If R is omitted, `FS' is used
  442.      instead.
  443.  
  444. `sprintf(FMT, EXPR-LIST)'
  445.      prints EXPR-LIST according to FMT, and returns the resulting
  446.      string.
  447.  
  448. `sub(R, S, T)'
  449.      this is just like `gsub', but only the first matching substring is
  450.      replaced.
  451.  
  452. `substr(S, I, N)'
  453.      returns the N-character substring of S starting at I.  If N is
  454.      omitted, the rest of S is used.
  455.  
  456. `tolower(STR)'
  457.      returns a copy of the string STR, with all the upper-case
  458.      characters in STR translated to their corresponding lower-case
  459.      counterparts.  Nonalphabetic characters are left unchanged.
  460.  
  461. `toupper(STR)'
  462.      returns a copy of the string STR, with all the lower-case
  463.      characters in STR translated to their corresponding upper-case
  464.      counterparts.  Nonalphabetic characters are left unchanged.
  465.  
  466. `system(CMD-LINE)'
  467.      Execute the command CMD-LINE, and return the exit status.
  468.  
  469. 
  470. File: gawk.info,  Node: Time Functions Summary,  Next: String Constants Summary,  Prev: String Functions Summary,  Up: Actions Summary
  471.  
  472. Built-in time functions
  473. .......................
  474.  
  475.    The following two functions are available for getting the current
  476. time of day, and for formatting time stamps.
  477.  
  478. `systime()'
  479.      returns the current time of day as the number of seconds since a
  480.      particular epoch (Midnight, January 1, 1970 UTC, on POSIX systems).
  481.  
  482. `strftime(FORMAT, TIMESTAMP)'
  483.      formats TIMESTAMP according to the specification in FORMAT.  The
  484.      current time of day is used if no TIMESTAMP is supplied.  *Note
  485.      Functions for Dealing with Time Stamps: Time Functions, for the
  486.      details on the conversion specifiers that `strftime' accepts.
  487.  
  488. 
  489. File: gawk.info,  Node: String Constants Summary,  Prev: Time Functions Summary,  Up: Actions Summary
  490.  
  491. String Constants
  492. ................
  493.  
  494.    String constants in `awk' are sequences of characters enclosed
  495. between double quotes (`"').  Within strings, certain "escape sequences"
  496. are recognized, as in C.  These are:
  497.  
  498. `\\'
  499.      A literal backslash.
  500.  
  501. `\a'
  502.      The "alert" character; usually the ASCII BEL character.
  503.  
  504. `\b'
  505.      Backspace.
  506.  
  507. `\f'
  508.      Formfeed.
  509.  
  510. `\n'
  511.      Newline.
  512.  
  513. `\r'
  514.      Carriage return.
  515.  
  516. `\t'
  517.      Horizontal tab.
  518.  
  519. `\v'
  520.      Vertical tab.
  521.  
  522. `\xHEX DIGITS'
  523.      The character represented by the string of hexadecimal digits
  524.      following the `\x'.  As in ANSI C, all following hexadecimal
  525.      digits are considered part of the escape sequence.  (This feature
  526.      should tell us something about language design by committee.)
  527.      E.g., `"\x1B"' is a string containing the ASCII ESC (escape)
  528.      character.  (The `\x' escape sequence is not in POSIX `awk'.)
  529.  
  530. `\DDD'
  531.      The character represented by the 1-, 2-, or 3-digit sequence of
  532.      octal digits.  Thus, `"\033"' is also a string containing the
  533.      ASCII ESC (escape) character.
  534.  
  535. `\C'
  536.      The literal character C.
  537.  
  538.    The escape sequences may also be used inside constant regular
  539. expressions (e.g., the regexp `/[ \t\f\n\r\v]/' matches whitespace
  540. characters).
  541.  
  542.    *Note Constant Expressions: Constants.
  543.  
  544. 
  545. File: gawk.info,  Node: Functions Summary,  Next: Historical Features,  Prev: Rules Summary,  Up: Gawk Summary
  546.  
  547. Functions
  548. =========
  549.  
  550.    Functions in `awk' are defined as follows:
  551.  
  552.      function NAME(PARAMETER LIST) { STATEMENTS }
  553.  
  554.    Actual parameters supplied in the function call are used to
  555. instantiate the formal parameters declared in the function.  Arrays are
  556. passed by reference, other variables are passed by value.
  557.  
  558.    If there are fewer arguments passed than there are names in
  559. PARAMETER-LIST, the extra names are given the null string as value.
  560. Extra names have the effect of local variables.
  561.  
  562.    The open-parenthesis in a function call of a user-defined function
  563. must immediately follow the function name, without any intervening
  564. white space.  This is to avoid a syntactic ambiguity with the
  565. concatenation operator.
  566.  
  567.    The word `func' may be used in place of `function' (but not in POSIX
  568. `awk').
  569.  
  570.    Use the `return' statement to return a value from a function.
  571.  
  572.    *Note User-defined Functions: User-defined, for a more complete
  573. description.
  574.  
  575. 
  576. File: gawk.info,  Node: Historical Features,  Prev: Functions Summary,  Up: Gawk Summary
  577.  
  578. Historical Features
  579. ===================
  580.  
  581.    There are two features of historical `awk' implementations that
  582. `gawk' supports.  First, it is possible to call the `length' built-in
  583. function not only with no arguments, but even without parentheses!
  584.  
  585.      a = length
  586.  
  587. is the same as either of
  588.  
  589.      a = length()
  590.      a = length($0)
  591.  
  592. This feature is marked as "deprecated" in the POSIX standard, and
  593. `gawk' will issue a warning about its use if `-W lint' is specified on
  594. the command line.
  595.  
  596.    The other feature is the use of the `continue' statement outside the
  597. body of a `while', `for', or `do' loop.  Traditional `awk'
  598. implementations have treated such usage as equivalent to the `next'
  599. statement.  `gawk' will support this usage if `-W posix' has not been
  600. specified.
  601.  
  602. 
  603. File: gawk.info,  Node: Sample Program,  Next: Bugs,  Prev: Gawk Summary,  Up: Top
  604.  
  605. Sample Program
  606. **************
  607.  
  608.    The following example is a complete `awk' program, which prints the
  609. number of occurrences of each word in its input.  It illustrates the
  610. associative nature of `awk' arrays by using strings as subscripts.  It
  611. also demonstrates the `for X in ARRAY' construction.  Finally, it shows
  612. how `awk' can be used in conjunction with other utility programs to do
  613. a useful task of some complexity with a minimum of effort.  Some
  614. explanations follow the program listing.
  615.  
  616.      awk '
  617.      # Print list of word frequencies
  618.      {
  619.          for (i = 1; i <= NF; i++)
  620.              freq[$i]++
  621.      }
  622.      
  623.      END {
  624.          for (word in freq)
  625.              printf "%s\t%d\n", word, freq[word]
  626.      }'
  627.  
  628.    The first thing to notice about this program is that it has two
  629. rules.  The first rule, because it has an empty pattern, is executed on
  630. every line of the input.  It uses `awk''s field-accessing mechanism
  631. (*note Examining Fields: Fields.) to pick out the individual words from
  632. the line, and the built-in variable `NF' (*note Built-in Variables::.)
  633. to know how many fields are available.
  634.  
  635.    For each input word, an element of the array `freq' is incremented to
  636. reflect that the word has been seen an additional time.
  637.  
  638.    The second rule, because it has the pattern `END', is not executed
  639. until the input has been exhausted.  It prints out the contents of the
  640. `freq' table that has been built up inside the first action.
  641.  
  642.    Note that this program has several problems that would prevent it
  643. from being useful by itself on real text files:
  644.  
  645.    * Words are detected using the `awk' convention that fields are
  646.      separated by whitespace and that other characters in the input
  647.      (except newlines) don't have any special meaning to `awk'.  This
  648.      means that punctuation characters count as part of words.
  649.  
  650.    * The `awk' language considers upper and lower case characters to be
  651.      distinct.  Therefore, `foo' and `Foo' are not treated by this
  652.      program as the same word.  This is undesirable since in normal
  653.      text, words are capitalized if they begin sentences, and a
  654.      frequency analyzer should not be sensitive to that.
  655.  
  656.    * The output does not come out in any useful order.  You're more
  657.      likely to be interested in which words occur most frequently, or
  658.      having an alphabetized table of how frequently each word occurs.
  659.  
  660.    The way to solve these problems is to use some of the more advanced
  661. features of the `awk' language.  First, we use `tolower' to remove case
  662. distinctions.  Next, we use `gsub' to remove punctuation characters.
  663. Finally, we use the system `sort' utility to process the output of the
  664. `awk' script.  First, here is the new version of the program:
  665.  
  666.      awk '
  667.      # Print list of word frequencies
  668.      {
  669.          $0 = tolower($0)    # remove case distinctions
  670.          gsub(/[^a-z0-9_ \t]/, "", $0)  # remove punctuation
  671.          for (i = 1; i <= NF; i++)
  672.              freq[$i]++
  673.      }
  674.      
  675.      END {
  676.          for (word in freq)
  677.              printf "%s\t%d\n", word, freq[word]
  678.      }'
  679.  
  680.    Assuming we have saved this program in a file named `frequency.awk',
  681. and that the data is in `file1', the following pipeline
  682.  
  683.      awk -f frequency.awk file1 | sort +1 -nr
  684.  
  685. produces a table of the words appearing in `file1' in order of
  686. decreasing frequency.
  687.  
  688.    The `awk' program suitably massages the data and produces a word
  689. frequency table, which is not ordered.
  690.  
  691.    The `awk' script's output is then sorted by the `sort' command and
  692. printed on the terminal.  The options given to `sort' in this example
  693. specify to sort using the second field of each input line (skipping one
  694. field), that the sort keys should be treated as numeric quantities
  695. (otherwise `15' would come before `5'), and that the sorting should be
  696. done in descending (reverse) order.
  697.  
  698.    We could have even done the `sort' from within the program, by
  699. changing the `END' action to:
  700.  
  701.      END {
  702.          sort = "sort +1 -nr"
  703.          for (word in freq)
  704.              printf "%s\t%d\n", word, freq[word] | sort
  705.          close(sort)
  706.      }'
  707.  
  708.    See the general operating system documentation for more information
  709. on how to use the `sort' command.
  710.  
  711. 
  712. File: gawk.info,  Node: Bugs,  Next: Notes,  Prev: Sample Program,  Up: Top
  713.  
  714. Reporting Problems and Bugs
  715. ***************************
  716.  
  717.    If you have problems with `gawk' or think that you have found a bug,
  718. please report it to the developers; we cannot promise to do anything
  719. but we might well want to fix it.
  720.  
  721.    Before reporting a bug, make sure you have actually found a real bug.
  722. Carefully reread the documentation and see if it really says you can do
  723. what you're trying to do.  If it's not clear whether you should be able
  724. to do something or not, report that too; it's a bug in the
  725. documentation!
  726.  
  727.    Before reporting a bug or trying to fix it yourself, try to isolate
  728. it to the smallest possible `awk' program and input data file that
  729. reproduces the problem.  Then send us the program and data file, some
  730. idea of what kind of Unix system you're using, and the exact results
  731. `gawk' gave you.  Also say what you expected to occur; this will help
  732. us decide whether the problem was really in the documentation.
  733.  
  734.    Once you have a precise problem, send e-mail to (Internet)
  735. `bug-gnu-utils@prep.ai.mit.edu' or (UUCP)
  736. `mit-eddie!prep.ai.mit.edu!bug-gnu-utils'.  Please include the version
  737. number of `gawk' you are using.  You can get this information with the
  738. command `gawk -W version '{}' /dev/null'.  You should send carbon
  739. copies of your mail to David Trueman at `david@cs.dal.ca', and to
  740. Arnold Robbins, who can be reached at `arnold@skeeve.atl.ga.us'.  David
  741. is most likely to fix code problems, while Arnold is most likely to fix
  742. documentation problems.
  743.  
  744.    Non-bug suggestions are always welcome as well.  If you have
  745. questions about things that are unclear in the documentation or are
  746. just obscure features, ask Arnold Robbins; he will try to help you out,
  747. although he may not have the time to fix the problem.  You can send him
  748. electronic mail at the Internet address above.
  749.  
  750.    If you find bugs in one of the non-Unix ports of `gawk', please send
  751. an electronic mail message to the person who maintains that port.  They
  752. are listed below, and also in the `README' file in the `gawk'
  753. distribution.  Information in the `README' file should be considered
  754. authoritative if it conflicts with this manual.
  755.  
  756.    The people maintaining the non-Unix ports of `gawk' are:
  757.  
  758. MS-DOS
  759.      The port to MS-DOS is maintained by Scott Deifik.  His electronic
  760.      mail address is `scottd@amgen.com'.
  761.  
  762. VMS
  763.      The port to VAX VMS is maintained by Pat Rankin.  His electronic
  764.      mail address is `rankin@eql.caltech.edu'.
  765.  
  766. Atari ST
  767.      The port to the Atari ST is maintained by Michal Jaegermann.  His
  768.      electronic mail address is `ntomczak@vm.ucs.ualberta.ca'.
  769.  
  770.    If your bug is also reproducible under Unix, please send copies of
  771. your report to the general GNU bug list, as well as to Arnold Robbins
  772. and David Trueman, at the addresses listed above.
  773.  
  774. 
  775. File: gawk.info,  Node: Notes,  Next: Glossary,  Prev: Bugs,  Up: Top
  776.  
  777. Implementation Notes
  778. ********************
  779.  
  780.    This appendix contains information mainly of interest to
  781. implementors and maintainers of `gawk'.  Everything in it applies
  782. specifically to `gawk', and not to other implementations.
  783.  
  784. * Menu:
  785.  
  786. * Compatibility Mode::          How to disable certain `gawk' extensions.
  787. * Future Extensions::           New features we may implement soon.
  788. * Improvements::                Suggestions for improvements by volunteers.
  789.  
  790. 
  791. File: gawk.info,  Node: Compatibility Mode,  Next: Future Extensions,  Prev: Notes,  Up: Notes
  792.  
  793. Downward Compatibility and Debugging
  794. ====================================
  795.  
  796.    *Note Extensions in `gawk' not in POSIX `awk': POSIX/GNU, for a
  797. summary of the GNU extensions to the `awk' language and program.  All
  798. of these features can be turned off by invoking `gawk' with the `-W
  799. compat' option, or with the `-W posix' option.
  800.  
  801.    If `gawk' is compiled for debugging with `-DDEBUG', then there is
  802. one more option available on the command line:
  803.  
  804. `-W parsedebug'
  805.      Print out the parse stack information as the program is being
  806.      parsed.
  807.  
  808.    This option is intended only for serious `gawk' developers, and not
  809. for the casual user.  It probably has not even been compiled into your
  810. version of `gawk', since it slows down execution.
  811.  
  812. 
  813. File: gawk.info,  Node: Future Extensions,  Next: Improvements,  Prev: Compatibility Mode,  Up: Notes
  814.  
  815. Probable Future Extensions
  816. ==========================
  817.  
  818.    This section briefly lists extensions that indicate the directions
  819. we are currently considering for `gawk'.  The file `FUTURES' in the
  820. `gawk' distributions lists these extensions, as well as several others.
  821.  
  822. `RS' as a regexp
  823.      The meaning of `RS' may be generalized along the lines of `FS'.
  824.  
  825. Control of subprocess environment
  826.      Changes made in `gawk' to the array `ENVIRON' may be propagated to
  827.      subprocesses run by `gawk'.
  828.  
  829. Databases
  830.      It may be possible to map a GDBM/NDBM/SDBM file into an `awk'
  831.      array.
  832.  
  833. Single-character fields
  834.      The null string, `""', as a field separator, will cause field
  835.      splitting and the `split' function to separate individual
  836.      characters.  Thus, `split(a, "abcd", "")' would yield `a[1] ==
  837.      "a"', `a[2] == "b"', and so on.
  838.  
  839. More `lint' warnings
  840.      There are more things that could be checked for portability.
  841.  
  842. `RECLEN' variable for fixed length records
  843.      Along with `FIELDWIDTHS', this would speed up the processing of
  844.      fixed-length records.
  845.  
  846. `RT' variable to hold the record terminator
  847.      It is occasionally useful to have access to the actual string of
  848.      characters that matched the `RS' variable.  The `RT' variable
  849.      would hold these characters.
  850.  
  851. A `restart' keyword
  852.      After modifying `$0', `restart' would restart the pattern matching
  853.      loop, without reading a new record from the input.
  854.  
  855. A `|&' redirection
  856.      The `|&' redirection, in place of `|', would open a two-way
  857.      pipeline for communication with a sub-process (via `getline' and
  858.      `print' and `printf').
  859.  
  860. `IGNORECASE' affecting all comparisons
  861.      The effects of the `IGNORECASE' variable may be generalized to all
  862.      string comparisons, and not just regular expression operations.
  863.  
  864. A way to mix command line source code and library files
  865.      There may be a new option that would make it possible to easily
  866.      use library functions from a program entered on the command line.
  867.  
  868. GNU-style long options
  869.      We will add GNU-style long options to `gawk' for compatibility
  870.      with other GNU programs.  (For example, `--field-separator=:'
  871.      would be equivalent to `-F:'.)
  872.  
  873. 
  874. File: gawk.info,  Node: Improvements,  Prev: Future Extensions,  Up: Notes
  875.  
  876. Suggestions for Improvements
  877. ============================
  878.  
  879.    Here are some projects that would-be `gawk' hackers might like to
  880. take on.  They vary in size from a few days to a few weeks of
  881. programming, depending on which one you choose and how fast a
  882. programmer you are.  Please send any improvements you write to the
  883. maintainers at the GNU project.
  884.  
  885.   1. Compilation of `awk' programs: `gawk' uses a Bison (YACC-like)
  886.      parser to convert the script given it into a syntax tree; the
  887.      syntax tree is then executed by a simple recursive evaluator.
  888.      This method incurs a lot of overhead, since the recursive
  889.      evaluator performs many procedure calls to do even the simplest
  890.      things.
  891.  
  892.      It should be possible for `gawk' to convert the script's parse tree
  893.      into a C program which the user would then compile, using the
  894.      normal C compiler and a special `gawk' library to provide all the
  895.      needed functions (regexps, fields, associative arrays, type
  896.      coercion, and so on).
  897.  
  898.      An easier possibility might be for an intermediate phase of `awk'
  899.      to convert the parse tree into a linear byte code form like the
  900.      one used in GNU Emacs Lisp.  The recursive evaluator would then be
  901.      replaced by a straight line byte code interpreter that would be
  902.      intermediate in speed between running a compiled program and doing
  903.      what `gawk' does now.
  904.  
  905.      This may actually happen for the 3.0 version of `gawk'.
  906.  
  907.   2. An error message section has not been included in this version of
  908.      the manual.  Perhaps some nice beta testers will document some of
  909.      the messages for the future.
  910.  
  911.   3. The programs in the test suite could use documenting in this
  912.      manual.
  913.  
  914.   4. The programs and data files in the manual should be available in
  915.      separate files to facilitate experimentation.
  916.  
  917.   5. See the `FUTURES' file for more ideas.  Contact us if you would
  918.      seriously like to tackle any of the items listed there.
  919.  
  920. 
  921. File: gawk.info,  Node: Glossary,  Next: Index,  Prev: Notes,  Up: Top
  922.  
  923. Glossary
  924. ********
  925.  
  926. Action
  927.      A series of `awk' statements attached to a rule.  If the rule's
  928.      pattern matches an input record, the `awk' language executes the
  929.      rule's action.  Actions are always enclosed in curly braces.
  930.      *Note Overview of Actions: Actions.
  931.  
  932. Amazing `awk' Assembler
  933.      Henry Spencer at the University of Toronto wrote a retargetable
  934.      assembler completely as `awk' scripts.  It is thousands of lines
  935.      long, including machine descriptions for several 8-bit
  936.      microcomputers.  It is a good example of a program that would have
  937.      been better written in another language.
  938.  
  939. ANSI
  940.      The American National Standards Institute.  This organization
  941.      produces many standards, among them the standard for the C
  942.      programming language.
  943.  
  944. Assignment
  945.      An `awk' expression that changes the value of some `awk' variable
  946.      or data object.  An object that you can assign to is called an
  947.      "lvalue".  *Note Assignment Expressions: Assignment Ops.
  948.  
  949. `awk' Language
  950.      The language in which `awk' programs are written.
  951.  
  952. `awk' Program
  953.      An `awk' program consists of a series of "patterns" and "actions",
  954.      collectively known as "rules".  For each input record given to the
  955.      program, the program's rules are all processed in turn.  `awk'
  956.      programs may also contain function definitions.
  957.  
  958. `awk' Script
  959.      Another name for an `awk' program.
  960.  
  961. Built-in Function
  962.      The `awk' language provides built-in functions that perform various
  963.      numerical, time stamp related, and string computations.  Examples
  964.      are `sqrt' (for the square root of a number) and `substr' (for a
  965.      substring of a string).  *Note Built-in Functions: Built-in.
  966.  
  967. Built-in Variable
  968.      `ARGC', `ARGIND', `ARGV', `CONVFMT', `ENVIRON', `ERRNO',
  969.      `FIELDWIDTHS', `FILENAME', `FNR', `FS', `IGNORECASE', `NF', `NR',
  970.      `OFMT', `OFS', `ORS', `RLENGTH', `RSTART', `RS', and `SUBSEP', are
  971.      the variables that have special meaning to `awk'.  Changing some
  972.      of them affects `awk''s running environment.  *Note Built-in
  973.      Variables::.
  974.  
  975. Braces
  976.      See "Curly Braces."
  977.  
  978. C
  979.      The system programming language that most GNU software is written
  980.      in.  The `awk' programming language has C-like syntax, and this
  981.      manual points out similarities between `awk' and C when
  982.      appropriate.
  983.  
  984. CHEM
  985.      A preprocessor for `pic' that reads descriptions of molecules and
  986.      produces `pic' input for drawing them.  It was written by Brian
  987.      Kernighan, and is available from `netlib@research.att.com'.
  988.  
  989. Compound Statement
  990.      A series of `awk' statements, enclosed in curly braces.  Compound
  991.      statements may be nested.  *Note Control Statements in Actions:
  992.      Statements.
  993.  
  994. Concatenation
  995.      Concatenating two strings means sticking them together, one after
  996.      another, giving a new string.  For example, the string `foo'
  997.      concatenated with the string `bar' gives the string `foobar'.
  998.      *Note String Concatenation: Concatenation.
  999.  
  1000. Conditional Expression
  1001.      An expression using the `?:' ternary operator, such as `EXPR1 ?
  1002.      EXPR2 : EXPR3'.  The expression EXPR1 is evaluated; if the result
  1003.      is true, the value of the whole expression is the value of EXPR2
  1004.      otherwise the value is EXPR3.  In either case, only one of EXPR2
  1005.      and EXPR3 is evaluated.  *Note Conditional Expressions:
  1006.      Conditional Exp.
  1007.  
  1008. Constant Regular Expression
  1009.      A constant regular expression is a regular expression written
  1010.      within slashes, such as `/foo/'.  This regular expression is chosen
  1011.      when you write the `awk' program, and cannot be changed doing its
  1012.      execution.  *Note How to Use Regular Expressions: Regexp Usage.
  1013.  
  1014. Comparison Expression
  1015.      A relation that is either true or false, such as `(a < b)'.
  1016.      Comparison expressions are used in `if', `while', and `for'
  1017.      statements, and in patterns to select which input records to
  1018.      process.  *Note Comparison Expressions: Comparison Ops.
  1019.  
  1020. Curly Braces
  1021.      The characters `{' and `}'.  Curly braces are used in `awk' for
  1022.      delimiting actions, compound statements, and function bodies.
  1023.  
  1024. Data Objects
  1025.      These are numbers and strings of characters.  Numbers are
  1026.      converted into strings and vice versa, as needed.  *Note
  1027.      Conversion of Strings and Numbers: Conversion.
  1028.  
  1029. Dynamic Regular Expression
  1030.      A dynamic regular expression is a regular expression written as an
  1031.      ordinary expression.  It could be a string constant, such as
  1032.      `"foo"', but it may also be an expression whose value may vary.
  1033.      *Note How to Use Regular Expressions: Regexp Usage.
  1034.  
  1035. Escape Sequences
  1036.      A special sequence of characters used for describing nonprinting
  1037.      characters, such as `\n' for newline, or `\033' for the ASCII ESC
  1038.      (escape) character.  *Note Constant Expressions: Constants.
  1039.  
  1040. Field
  1041.      When `awk' reads an input record, it splits the record into pieces
  1042.      separated by whitespace (or by a separator regexp which you can
  1043.      change by setting the built-in variable `FS').  Such pieces are
  1044.      called fields.  If the pieces are of fixed length, you can use the
  1045.      built-in variable `FIELDWIDTHS' to describe their lengths.  *Note
  1046.      How Input is Split into Records: Records.
  1047.  
  1048. Format
  1049.      Format strings are used to control the appearance of output in the
  1050.      `printf' statement.  Also, data conversions from numbers to strings
  1051.      are controlled by the format string contained in the built-in
  1052.      variable `CONVFMT'.  *Note Format-Control Letters: Control Letters.
  1053.  
  1054. Function
  1055.      A specialized group of statements often used to encapsulate general
  1056.      or program-specific tasks.  `awk' has a number of built-in
  1057.      functions, and also allows you to define your own.  *Note Built-in
  1058.      Functions: Built-in.  Also, see *Note User-defined Functions:
  1059.      User-defined.
  1060.  
  1061. `gawk'
  1062.      The GNU implementation of `awk'.
  1063.  
  1064. GNU
  1065.      "GNU's not Unix".  An on-going project of the Free Software
  1066.      Foundation to create a complete, freely distributable,
  1067.      POSIX-compliant computing environment.
  1068.  
  1069. Input Record
  1070.      A single chunk of data read in by `awk'.  Usually, an `awk' input
  1071.      record consists of one line of text.  *Note How Input is Split
  1072.      into Records: Records.
  1073.  
  1074. Keyword
  1075.      In the `awk' language, a keyword is a word that has special
  1076.      meaning.  Keywords are reserved and may not be used as variable
  1077.      names.
  1078.  
  1079.      `awk''s keywords are: `if', `else', `while', `do...while', `for',
  1080.      `for...in', `break', `continue', `delete', `next', `function',
  1081.      `func', and `exit'.
  1082.  
  1083. Lvalue
  1084.      An expression that can appear on the left side of an assignment
  1085.      operator.  In most languages, lvalues can be variables or array
  1086.      elements.  In `awk', a field designator can also be used as an
  1087.      lvalue.
  1088.  
  1089. Number
  1090.      A numeric valued data object.  The `gawk' implementation uses
  1091.      double precision floating point to represent numbers.
  1092.  
  1093. Pattern
  1094.      Patterns tell `awk' which input records are interesting to which
  1095.      rules.
  1096.  
  1097.      A pattern is an arbitrary conditional expression against which
  1098.      input is tested.  If the condition is satisfied, the pattern is
  1099.      said to "match" the input record.  A typical pattern might compare
  1100.      the input record against a regular expression.  *Note Patterns::.
  1101.  
  1102. POSIX
  1103.      The name for a series of standards being developed by the IEEE
  1104.      that specify a Portable Operating System interface.  The "IX"
  1105.      denotes the Unix heritage of these standards.  The main standard
  1106.      of interest for `awk' users is P1003.2, the Command Language and
  1107.      Utilities standard.
  1108.  
  1109. Range (of input lines)
  1110.      A sequence of consecutive lines from the input file.  A pattern
  1111.      can specify ranges of input lines for `awk' to process, or it can
  1112.      specify single lines.  *Note Patterns::.
  1113.  
  1114. Recursion
  1115.      When a function calls itself, either directly or indirectly.  If
  1116.      this isn't clear, refer to the entry for "recursion."
  1117.  
  1118. Redirection
  1119.      Redirection means performing input from other than the standard
  1120.      input stream, or output to other than the standard output stream.
  1121.  
  1122.      You can redirect the output of the `print' and `printf' statements
  1123.      to a file or a system command, using the `>', `>>', and `|'
  1124.      operators.  You can redirect input to the `getline' statement using
  1125.      the `<' and `|' operators.  *Note Redirecting Output of `print'
  1126.      and `printf': Redirection.
  1127.  
  1128. Regular Expression
  1129.      See "regexp."
  1130.  
  1131. Regexp
  1132.      Short for "regular expression".  A regexp is a pattern that
  1133.      denotes a set of strings, possibly an infinite set.  For example,
  1134.      the regexp `R.*xp' matches any string starting with the letter `R'
  1135.      and ending with the letters `xp'.  In `awk', regexps are used in
  1136.      patterns and in conditional expressions.  Regexps may contain
  1137.      escape sequences.  *Note Regular Expressions as Patterns: Regexp.
  1138.  
  1139. Rule
  1140.      A segment of an `awk' program, that specifies how to process single
  1141.      input records.  A rule consists of a "pattern" and an "action".
  1142.      `awk' reads an input record; then, for each rule, if the input
  1143.      record satisfies the rule's pattern, `awk' executes the rule's
  1144.      action.  Otherwise, the rule does nothing for that input record.
  1145.  
  1146. Side Effect
  1147.      A side effect occurs when an expression has an effect aside from
  1148.      merely producing a value.  Assignment expressions, increment
  1149.      expressions and function calls have side effects.  *Note
  1150.      Assignment Expressions: Assignment Ops.
  1151.  
  1152. Special File
  1153.      A file name interpreted internally by `gawk', instead of being
  1154.      handed directly to the underlying operating system.  For example,
  1155.      `/dev/stdin'.  *Note Standard I/O Streams: Special Files.
  1156.  
  1157. Stream Editor
  1158.      A program that reads records from an input stream and processes
  1159.      them one or more at a time.  This is in contrast with batch
  1160.      programs, which may expect to read their input files in entirety
  1161.      before starting to do anything, and with interactive programs,
  1162.      which require input from the user.
  1163.  
  1164. String
  1165.      A datum consisting of a sequence of characters, such as `I am a
  1166.      string'.  Constant strings are written with double-quotes in the
  1167.      `awk' language, and may contain escape sequences.  *Note Constant
  1168.      Expressions: Constants.
  1169.  
  1170. Whitespace
  1171.      A sequence of blank or tab characters occurring inside an input
  1172.      record or a string.
  1173.  
  1174.