home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / gawk.info-2 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  51KB  |  945 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: Statements/Lines,  Next: When,  Prev: Comments,  Up: Getting Started
  21. `awk' Statements versus Lines
  22. =============================
  23.    Most often, each line in an `awk' program is a separate statement or
  24. separate rule, like this:
  25.      awk '/12/  { print $0 }
  26.           /21/  { print $0 }' BBS-list inventory-shipped
  27.    But sometimes statements can be more than one line, and lines can
  28. contain several statements.  You can split a statement into multiple
  29. lines by inserting a newline after any of the following:
  30.      ,    {    ?    :    ||    &&    do    else
  31. A newline at any other point is considered the end of the statement.
  32. (Splitting lines after `?' and `:' is a minor `gawk' extension.  The
  33. `?' and `:' referred to here is the three operand conditional
  34. expression described in *Note Conditional Expressions: Conditional Exp.)
  35.    If you would like to split a single statement into two lines at a
  36. point where a newline would terminate it, you can "continue" it by
  37. ending the first line with a backslash character, `\'.  This is allowed
  38. absolutely anywhere in the statement, even in the middle of a string or
  39. regular expression.  For example:
  40.      awk '/This program is too long, so continue it\
  41.       on the next line/ { print $1 }'
  42. We have generally not used backslash continuation in the sample
  43. programs in this manual.  Since in `gawk' there is no limit on the
  44. length of a line, it is never strictly necessary; it just makes
  45. programs prettier.  We have preferred to make them even more pretty by
  46. keeping the statements short.  Backslash continuation is most useful
  47. when your `awk' program is in a separate source file, instead of typed
  48. in on the command line.  You should also note that many `awk'
  49. implementations are more picky about where you may use backslash
  50. continuation.  For maximal portability of your `awk' programs, it is
  51. best not to split your lines in the middle of a regular expression or a
  52. string.
  53.    *Warning: backslash continuation does not work as described above
  54. with the C shell.*  Continuation with backslash works for `awk'
  55. programs in files, and also for one-shot programs *provided* you are
  56. using a POSIX-compliant shell, such as the Bourne shell or the
  57. Bourne-again shell.  But the C shell used on Berkeley Unix behaves
  58. differently!  There, you must use two backslashes in a row, followed by
  59. a newline.
  60.    When `awk' statements within one rule are short, you might want to
  61. put more than one of them on a line.  You do this by separating the
  62. statements with a semicolon, `;'.  This also applies to the rules
  63. themselves.  Thus, the previous program could have been written:
  64.      /12/ { print $0 } ; /21/ { print $0 }
  65. *Note:* the requirement that rules on the same line must be separated
  66. with a semicolon is a recent change in the `awk' language; it was done
  67. for consistency with the treatment of statements within an action.
  68. File: gawk.info,  Node: When,  Prev: Statements/Lines,  Up: Getting Started
  69. When to Use `awk'
  70. =================
  71.    You might wonder how `awk' might be useful for you.  Using additional
  72. utility programs, more advanced patterns, field separators, arithmetic
  73. statements, and other selection criteria, you can produce much more
  74. complex output.  The `awk' language is very useful for producing
  75. reports from large amounts of raw data, such as summarizing information
  76. from the output of other utility programs like `ls'.  (*Note A More
  77. Complex Example: More Complex.)
  78.    Programs written with `awk' are usually much smaller than they would
  79. be in other languages.  This makes `awk' programs easy to compose and
  80. use.  Often `awk' programs can be quickly composed at your terminal,
  81. used once, and thrown away.  Since `awk' programs are interpreted, you
  82. can avoid the usually lengthy edit-compile-test-debug cycle of software
  83. development.
  84.    Complex programs have been written in `awk', including a complete
  85. retargetable assembler for 8-bit microprocessors (*note Glossary::., for
  86. more information) and a microcode assembler for a special purpose Prolog
  87. computer.  However, `awk''s capabilities are strained by tasks of such
  88. complexity.
  89.    If you find yourself writing `awk' scripts of more than, say, a few
  90. hundred lines, you might consider using a different programming
  91. language.  Emacs Lisp is a good choice if you need sophisticated string
  92. or pattern matching capabilities.  The shell is also good at string and
  93. pattern matching; in addition, it allows powerful use of the system
  94. utilities.  More conventional languages, such as C, C++, and Lisp, offer
  95. better facilities for system programming and for managing the complexity
  96. of large programs.  Programs in these languages may require more lines
  97. of source code than the equivalent `awk' programs, but they are easier
  98. to maintain and usually run more efficiently.
  99. File: gawk.info,  Node: Reading Files,  Next: Printing,  Prev: Getting Started,  Up: Top
  100. Reading Input Files
  101. *******************
  102.    In the typical `awk' program, all input is read either from the
  103. standard input (by default the keyboard, but often a pipe from another
  104. command) or from files whose names you specify on the `awk' command
  105. line.  If you specify input files, `awk' reads them in order, reading
  106. all the data from one before going on to the next.  The name of the
  107. current input file can be found in the built-in variable `FILENAME'
  108. (*note Built-in Variables::.).
  109.    The input is read in units called records, and processed by the
  110. rules one record at a time.  By default, each record is one line.  Each
  111. record is split automatically into fields, to make it more convenient
  112. for a rule to work on its parts.
  113.    On rare occasions you will need to use the `getline' command, which
  114. can do explicit input from any number of files (*note Explicit Input
  115. with `getline': Getline.).
  116. * Menu:
  117. * Records::                     Controlling how data is split into records.
  118. * Fields::                      An introduction to fields.
  119. * Non-Constant Fields::         Non-constant Field Numbers.
  120. * Changing Fields::             Changing the Contents of a Field.
  121. * Field Separators::            The field separator and how to change it.
  122. * Constant Size::               Reading constant width data.
  123. * Multiple Line::               Reading multi-line records.
  124. * Getline::                     Reading files under explicit program control
  125.                                 using the `getline' function.
  126. * Close Input::                 Closing an input file (so you can read from
  127.                                 the beginning once more).
  128. File: gawk.info,  Node: Records,  Next: Fields,  Prev: Reading Files,  Up: Reading Files
  129. How Input is Split into Records
  130. ===============================
  131.    The `awk' language divides its input into records and fields.
  132. Records are separated by a character called the "record separator".  By
  133. default, the record separator is the newline character, defining a
  134. record to be a single line of text.
  135.    Sometimes you may want to use a different character to separate your
  136. records.  You can use a different character by changing the built-in
  137. variable `RS'.  The value of `RS' is a string that says how to separate
  138. records; the d