home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / gawk.info-5 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  49KB  |  959 lines

  1. This is Info file gawk.info, produced by Makeinfo-1.55 from the input
  2. file /gnu/src/amiga/gawk-2.15.5/gawk.texi.
  3.    This file documents `awk', a program that you can use to select
  4. particular records in a file and perform operations upon them.
  5.    This is Edition 0.15 of `The GAWK Manual',
  6. for the 2.15 version of the GNU implementation
  7. of AWK.
  8.    Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that
  14. the entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20. File: gawk.info,  Node: For Statement,  Next: Break Statement,  Prev: Do Statement,  Up: Statements
  21. The `for' Statement
  22. ===================
  23.    The `for' statement makes it more convenient to count iterations of a
  24. loop.  The general form of the `for' statement looks like this:
  25.      for (INITIALIZATION; CONDITION; INCREMENT)
  26.        BODY
  27. This statement starts by executing INITIALIZATION.  Then, as long as
  28. CONDITION is true, it repeatedly executes BODY and then INCREMENT.
  29. Typically INITIALIZATION sets a variable to either zero or one,
  30. INCREMENT adds 1 to it, and CONDITION compares it against the desired
  31. number of iterations.
  32.    Here is an example of a `for' statement:
  33.      awk '{ for (i = 1; i <= 3; i++)
  34.                print $i
  35.      }'
  36. This prints the first three fields of each input record, one field per
  37. line.
  38.    In the `for' statement, BODY stands for any statement, but
  39. INITIALIZATION, CONDITION and INCREMENT are just expressions.  You
  40. cannot set more than one variable in the INITIALIZATION part unless you
  41. use a multiple assignment statement such as `x = y = 0', which is
  42. possible only if all the initial values are equal.  (But you can
  43. initialize additional variables by writing their assignments as
  44. separate statements preceding the `for' loop.)
  45.    The same is true of the INCREMENT part; to increment additional
  46. variables, you must write separate statements at the end of the loop.
  47. The C compound expression, using C's comma operator, would be useful in
  48. this context, but it is not supported in `awk'.
  49.    Most often, INCREMENT is an increment expression, as in the example
  50. above.  But this is not required; it can be any expression whatever.
  51. For example, this statement prints all the powers of 2 between 1 and
  52.      for (i = 1; i <= 100; i *= 2)
  53.        print i
  54.    Any of the three expressions in the parentheses following the `for'
  55. may be omitted if there is nothing to be done there.  Thus,
  56. `for (;x > 0;)' is equivalent to `while (x > 0)'.  If the CONDITION is
  57. omitted, it is treated as TRUE, effectively yielding an "infinite loop"
  58. (i.e., a loop that will never terminate).
  59.    In most cases, a `for' loop is an abbreviation for a `while' loop,
  60. as shown here:
  61.      INITIALIZATION
  62.      while (CONDITION) {
  63.        BODY
  64.        INCREMENT
  65.      }
  66. The only exception is when the `continue' statement (*note The
  67. `continue' Statement: Continue Statement.) is used inside the loop;
  68. changing a `for' statement to a `while' statement in this way can
  69. change the effect of the `continue' statement inside the loop.
  70.    There is an alternate version of the `for' loop, for iterating over
  71. all the indices of an array:
  72.      for (i in array)
  73.          DO SOMETHING WITH array[i]
  74. *Note Arrays in `awk': Arrays, for more information on this version of
  75. the `for' loop.
  76.    The `awk' language has a `for' statement in addition to a `while'
  77. statement because often a `for' loop is both less work to type and more
  78. natural to think of.  Counting the number of iterations is very common
  79. in loops.  It can be easier to think of this counting as part of
  80. looping rather than as something to do inside the loop.
  81.    The next section has more complicated examples of `for' loops.
  82. File: gawk.info,  Node: Break Statement,  Next: Continue Statement,  Prev: For Statement,  Up: Statements
  83. The `break' Statement
  84. =====================
  85.    The `break' statement jumps out of the innermost `for', `while', or
  86. `do'-`while' loop that encloses it.  The following example finds the
  87. smallest divisor of any integer, and also identifies prime numbers:
  88.      awk '# find smallest divisor of num
  89.           { num = $1
  90.             for (div = 2; div*div <= num; div++)
  91.               if (num % div == 0)
  92.                 break
  93.             if (num % div == 0)
  94.               printf "Smallest divisor of %d is %d\n", num, div
  95.             else
  96.               printf "%d is prime\n", num  }'
  97.    When the remainder is zero in the first `if' statement, `awk'
  98. immediately "breaks out" of the containing `for' loop.  This means that
  99. `awk' proceeds immediately to the statement following the loop and
  100. continues processing.  (This is very different from the `exit'
  101. statement which stops the entire `awk' program.  *Note The `exit'
  102. Statement: Exit Statement.)
  103.    Here is another program equivalent to the previous one.  It
  104. illustrates how the CONDITION of a `for' or `while' could just as well
  105. be replaced with a `break' inside an `if':
  106.      awk '# find smallest divisor of num
  107.           { num = $1
  108.             for (div = 2; ; div++) {
  109.               if (num % div == 0) {
  110.                 printf "Smallest divisor of %d is %d\n", num, div
  111.                 break
  112.               }
  113.               if (div*div > num) {
  114.                 printf "%d is prime\n", num
  115.                 break
  116.               }
  117.             }
  118.      }'
  119. File: gawk.info,  Node: Continue Statement,  Next: Next Statement,  Prev: Break Statement,  Up: Statements
  120. The `continue' Statement
  121. ========================
  122.    The `continue' statement, like `break', is used only inside `for',
  123. `while', and `do'-`while' loops.  It skips over the rest of the loop
  124. body, causing the next cycle around the loop to begin immediately.
  125. Contrast this with `break', which jumps out of the loop altogether.
  126. Here is an example:
  127.      # print names that don't contain the string "ignore"
  128.      
  129.      # first, save the text of each line
  130.      { names[NR] = $0 }
  131.      
  132.      # print what we're interested in
  133.      END {
  134.         for (x in names) {
  135.             if (names[x] ~ /ignore/)
  136.                 continue
  137.             print names[x]
  138.         }
  139.      }
  140.    If one of the input records contains the string `ignore', this
  141. example skips the print statement for that record, and continues back to
  142. the first statement in the loop.
  143.    This is not a practical example of `continue', since it would be
  144. just as easy to write the loop like this:
  145.      for (x in names)
  146.        if (names[x] !~ /ignore/)
  147.          print names[x]
  148.    The `continue' statement in a `for' loop directs `awk' to skip the
  149. rest of the body of the loop, and resume execution with the
  150. increment-expression of the `for' statement.  The following program
  151. illustrates this fact:
  152.      awk 'BEGIN {
  153.           for (x = 0; x <= 20; x++) {
  154.               if (x == 5)
  155.                   continue
  156.               printf ("%d ", x)
  157.           }
  158.           print ""
  159.      }'
  160. This program prints all the numbers from 0 to 20, except for 5, for
  161. which the `printf' is skipped.  Since the increment `x++' is not
  162. skipped, `x' does not remain stuck at 5.  Contrast the `for' loop above
  163. with the `while' loop:
  164.      awk 'BEGIN {
  165.           x = 0
  166.           while (x <= 20) {
  167.               if (x == 5)
  168.                   continue
  169.               printf ("%d ", x)
  170.               x++
  171.           }
  172.           print ""
  173.      }'
  174. This program loops forever once `x' gets to 5.
  175.    As described above, the `continue' statement has no meaning when
  176. used outside the body of a loop.  However, although it was never
  177. documented, historical implementations of `awk' have treated the
  178. `continue' statement outside of a loop as if it were a `next' statement
  179. (*note The `next' Statement: Next Statement.).  By default, `gawk'
  180. silently s