home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / unix / gawk_doc.zoo / gawk-info-1 < prev    next >
Encoding:
Text File  |  1989-04-13  |  49.9 KB  |  1,232 lines

  1. Info file gawk-info, produced by Makeinfo, -*- Text -*- from input
  2. file gawk.texinfo.
  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. Copyright (C) 1989 Free Software Foundation, Inc.
  8.  
  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.  
  13. Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided that
  15. the entire resulting derived work is distributed under the terms of a
  16. permission notice identical to this one.
  17.  
  18. Permission is granted to copy and distribute translations of this
  19. manual into another language, under the above conditions for modified
  20. versions, except that this permission notice may be stated in a
  21. translation approved by the Foundation.
  22.  
  23.  
  24. 
  25. File: gawk-info,  Node: Top,  Next: Preface,  Prev: (dir),  Up: (dir)
  26.  
  27. This file documents `awk', a program that you can use to select
  28. particular records in a file and perform operations upon them; it
  29. contains the following chapters:
  30.  
  31. * Menu:
  32.  
  33. * Preface::           What you can do with `awk'; brief history
  34.                and acknowledgements.
  35.  
  36. * License::           Your right to copy and distribute `gawk'.
  37.  
  38. * This Manual::           Using this manual.
  39.  
  40.                Includes sample input files that you can use.
  41.  
  42. * Getting Started::    A basic introduction to using `awk'.
  43.                        How to run an `awk' program.  Command line syntax.
  44.  
  45. * Reading Files::      How to read files and manipulate fields.
  46.  
  47. * Printing::           How to print using `awk'.  Describes the
  48.                        `print' and `printf' statements.  
  49.                Also describes redirection of output.
  50.  
  51. * One-liners::         Short, sample `awk' programs.
  52.  
  53. * Patterns::           The various types of patterns explained in detail.
  54.  
  55. * Actions::            The various types of actions are introduced here.
  56.                        Describes expressions and the various operators in
  57.                        detail.  Also describes comparison expressions.
  58.  
  59. * Statements::         The various control statements are described in
  60.                        detail.
  61.  
  62. * Arrays::             The description and use of arrays.  Also includes
  63.                        array--oriented control statements.
  64.  
  65. * User-defined::       User--defined functions are described in detail.
  66.  
  67. * Built-in::           The built--in functions are summarized here.
  68.  
  69. * Special::            The special variables are summarized here.
  70.  
  71. * Sample Program::     A sample `awk' program with a complete explanation.
  72.  
  73. * Notes::              Something about the implementation of `gawk'.
  74.  
  75. * Glossary::           An explanation of some unfamiliar terms.
  76.  
  77. * Index::
  78.  
  79.  
  80. 
  81. File: gawk-info,  Node: Preface,  Next: License,  Prev: Top,  Up: Top
  82.  
  83. Preface
  84. *******
  85.  
  86. If you are like many computer users, you frequently would like to
  87. make changes in various text files wherever certain patterns appear,
  88. or extract data from parts of certain lines while discarding the
  89. rest.  To write a program to do this in a language such as C or
  90. Pascal is a time--consuming inconvenience that may take many lines of
  91. code.  The job may be easier with `awk'.
  92.  
  93. The `awk' utility interprets a special--purpose programming language
  94. that makes it possible to handle simple data--reformatting jobs
  95. easily with just a few lines of code.
  96.  
  97. The GNU implementation of `awk' is called `gawk'; it is fully upward
  98. compatible with the System V Release 3.1 and later version of `awk'. 
  99. All properly written `awk' programs should work with `gawk'.  So we
  100. usually don't distinguish between `gawk' and other `awk'
  101. implementations in this manual.
  102.  
  103. This manual teaches you what `awk' does and how you can use `awk'
  104. effectively.  You should already be familiar with basic,
  105. general--purpose, operating system commands such as `ls'.  Using
  106. `awk' you can:
  107.  
  108.    * manage small, personal databases,
  109.  
  110.    * generate reports,
  111.  
  112.    * validate data,
  113.  
  114.    * produce indexes, and perform other document preparation tasks,
  115.  
  116.    * even experiment with algorithms that can be adapted later to
  117.      other computer languages!
  118.  
  119. * Menu:
  120.  
  121. * History::  The history of gawk and awk.  Acknowledgements.
  122.  
  123.  
  124. 
  125. File: gawk-info,  Node: History,  Up: Preface
  126.  
  127. History of `awk' and `gawk'
  128. ===========================
  129.  
  130. The name `awk' comes from the initials of its designers: Alfred V. 
  131. Aho, Peter J. Weinberger, and Brian W. Kernighan.  The original
  132. version of `awk' was written in 1977.  In 1985 a new version made the
  133. programming language more powerful, introducing user--defined
  134. functions, multiple input streams, and computed regular expressions.
  135.  
  136. The GNU implementation, `gawk', was written in 1986 by Paul Rubin and
  137. Jay Fenlason, with advice from Richard Stallman.  John Woods
  138. contributed parts of the code as well.  In 1988, David Trueman, with
  139. help from Arnold Robbins, reworked `gawk' for compatibility with the
  140. newer `awk'.
  141.  
  142. Many people need to be thanked for their assistance in producing this
  143. manual.  Jay Fenlason contributed many ideas and sample programs. 
  144. Richard Mlynarik and Robert Chassell gave helpful comments on drafts
  145. of this manual.  The paper ``A Supplemental Document for `awk''' by
  146. John W.  Pierce of the Chemistry Department at UC San Diego,
  147. pinpointed several issues relevant both to `awk' implementation and
  148. to this manual, that would otherwise have escaped us.
  149.  
  150. Finally, we would like to thank Brian Kernighan of Bell Labs for
  151. invaluable assistance during the testing and debugging of `gawk', and
  152. for help in clarifying several points about the language.
  153.  
  154.  
  155. 
  156. File: gawk-info,  Node: License,  Next: This Manual,  Prev: Preface,  Up: Top
  157.  
  158. GNU GENERAL PUBLIC LICENSE
  159. **************************
  160.  
  161.                         Version 1, February 1989
  162.  
  163.      Copyright (C) 1989 Free Software Foundation, Inc.
  164.      675 Mass Ave, Cambridge, MA 02139, USA
  165.      
  166.      Everyone is permitted to copy and distribute verbatim copies
  167.      of this license document, but changing it is not allowed.
  168.  
  169.  Preamble
  170. =========
  171.  
  172.   The license agreements of most software companies try to keep users
  173. at the mercy of those companies.  By contrast, our General Public
  174. License is intended to guarantee your freedom to share and change
  175. free software--to make sure the software is free for all its users. 
  176. The General Public License applies to the Free Software Foundation's
  177. software and to any other program whose authors commit to using it. 
  178. You can use it for your programs, too.
  179.  
  180.   When we speak of free software, we are referring to freedom, not
  181. price.  Specifically, the General Public License is designed to make
  182. sure that you have the freedom to give away or sell copies of free
  183. software, that you receive source code or can get it if you want it,
  184. that you can change the software or use pieces of it in new free
  185. programs; and that you know you can do these things.
  186.  
  187.   To protect your rights, we need to make restrictions that forbid
  188. anyone to deny you these rights or to ask you to surrender the rights.
  189. These restrictions translate to certain responsibilities for you if
  190. you distribute copies of the software, or if you modify it.
  191.  
  192.   For example, if you distribute copies of a such a program, whether
  193. gratis or for a fee, you must give the recipients all the rights that
  194. you have.  You must make sure that they, too, receive or can get the
  195. source code.  And you must tell them their rights.
  196.  
  197.   We protect your rights with two steps: (1) copyright the software,
  198. and (2) offer you this license which gives you legal permission to
  199. copy, distribute and/or modify the software.
  200.  
  201.   Also, for each author's protection and ours, we want to make certain
  202. that everyone understands that there is no warranty for this free
  203. software.  If the software is modified by someone else and passed on,
  204. we want its recipients to know that what they have is not the
  205. original, so that any problems introduced by others will not reflect
  206. on the original authors' reputations.
  207.  
  208.   The precise terms and conditions for copying, distribution and
  209. modification follow.
  210.  
  211.                           TERMS AND CONDITIONS
  212.  
  213.   1. This License Agreement applies to any program or other work
  214.      which contains a notice placed by the copyright holder saying it
  215.      may be distributed under the terms of this General Public
  216.      License.  The ``Program'', below, refers to any such program or
  217.      work, and a ``work based on the Program'' means either the
  218.      Program or any work containing the Program or a portion of it,
  219.      either verbatim or with modifications.  Each licensee is
  220.      addressed as ``you''.
  221.  
  222.   2. You may copy and distribute verbatim copies of the Program's
  223.      source code as you receive it, in any medium, provided that you
  224.      conspicuously and appropriately publish on each copy an
  225.      appropriate copyright notice and disclaimer of warranty; keep
  226.      intact all the notices that refer to this General Public License
  227.      and to the absence of any warranty; and give any other
  228.      recipients of the Program a copy of this General Public License
  229.      along with the Program.  You may charge a fee for the physical
  230.      act of transferring a copy.
  231.  
  232.   3. You may modify your copy or copies of the Program or any portion
  233.      of it, and copy and distribute such modifications under the
  234.      terms of Paragraph 1 above, provided that you also do the
  235.      following:
  236.  
  237.         * cause the modified files to carry prominent notices stating
  238.           that you changed the files and the date of any change; and
  239.  
  240.         * cause the whole of any work that you distribute or publish,
  241.           that in whole or in part contains the Program or any part
  242.           thereof, either with or without modifications, to be
  243.           licensed at no charge to all third parties under the terms
  244.           of this General Public License (except that you may choose
  245.           to grant warranty protection to some or all third parties,
  246.           at your option).
  247.  
  248.         * If the modified program normally reads commands
  249.           interactively when run, you must cause it, when started
  250.           running for such interactive use in the simplest and most
  251.           usual way, to print or display an announcement including an
  252.           appropriate copyright notice and a notice that there is no
  253.           warranty (or else, saying that you provide a warranty) and
  254.           that users may redistribute the program under these
  255.           conditions, and telling the user how to view a copy of this
  256.           General Public License.
  257.  
  258.         * You may charge a fee for the physical act of transferring a
  259.           copy, and you may at your option offer warranty protection
  260.           in exchange for a fee.
  261.  
  262.      Mere aggregation of another independent work with the Program
  263.      (or its derivative) on a volume of a storage or distribution
  264.      medium does not bring the other work under the scope of these
  265.      terms.
  266.  
  267.   4. You may copy and distribute the Program (or a portion or
  268.      derivative of it, under Paragraph 2) in object code or
  269.      executable form under the terms of Paragraphs 1 and 2 above
  270.      provided that you also do one of the following:
  271.  
  272.         * accompany it with the complete corresponding
  273.           machine-readable source code, which must be distributed
  274.           under the terms of Paragraphs 1 and 2 above; or,
  275.  
  276.         * accompany it with a written offer, valid for at least three
  277.           years, to give any third party free (except for a nominal
  278.           charge for the cost of distribution) a complete
  279.           machine-readable copy of the corresponding source code, to
  280.           be distributed under the terms of Paragraphs 1 and 2 above;
  281.           or,
  282.  
  283.         * accompany it with the information you received as to where
  284.           the corresponding source code may be obtained.  (This
  285.           alternative is allowed only for noncommercial distribution
  286.           and only if you received the program in object code or
  287.           executable form alone.)
  288.  
  289.      Source code for a work means the preferred form of the work for
  290.      making modifications to it.  For an executable file, complete
  291.      source code means all the source code for all modules it
  292.      contains; but, as a special exception, it need not include
  293.      source code for modules which are standard libraries that
  294.      accompany the operating system on which the executable file
  295.      runs, or for standard header files or definitions files that
  296.      accompany that operating system.
  297.  
  298.   5. You may not copy, modify, sublicense, distribute or transfer the
  299.      Program except as expressly provided under this General Public
  300.      License.  Any attempt otherwise to copy, modify, sublicense,
  301.      distribute or transfer the Program is void, and will
  302.      automatically terminate your rights to use the Program under
  303.      this License.  However, parties who have received copies, or
  304.      rights to use copies, from you under this General Public License
  305.      will not have their licenses terminated so long as such parties
  306.      remain in full compliance.
  307.  
  308.   6. By copying, distributing or modifying the Program (or any work
  309.      based on the Program) you indicate your acceptance of this
  310.      license to do so, and all its terms and conditions.
  311.  
  312.   7. Each time you redistribute the Program (or any work based on the
  313.      Program), the recipient automatically receives a license from
  314.      the original licensor to copy, distribute or modify the Program
  315.      subject to these terms and conditions.  You may not impose any
  316.      further restrictions on the recipients' exercise of the rights
  317.      granted herein.
  318.  
  319.   8. The Free Software Foundation may publish revised and/or new
  320.      versions of the General Public License from time to time.  Such
  321.      new versions will be similar in spirit to the present version,
  322.      but may differ in detail to address new problems or concerns.
  323.  
  324.      Each version is given a distinguishing version number.  If the
  325.      Program specifies a version number of the license which applies
  326.      to it and ``any later version'', you have the option of
  327.      following the terms and conditions either of that version or of
  328.      any later version published by the Free Software Foundation.  If
  329.      the Program does not specify a version number of the license,
  330.      you may choose any version ever published by the Free Software
  331.      Foundation.
  332.  
  333.   9. If you wish to incorporate parts of the Program into other free
  334.      programs whose distribution conditions are different, write to
  335.      the author to ask for permission.  For software which is
  336.      copyrighted by the Free Software Foundation, write to the Free
  337.      Software Foundation; we sometimes make exceptions for this.  Our
  338.      decision will be guided by the two goals of preserving the free
  339.      status of all derivatives of our free software and of promoting
  340.      the sharing and reuse of software generally.
  341.  
  342.                                    NO WARRANTY
  343.  
  344.  10. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
  345.      WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
  346.      LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  347.      HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM ``AS IS''
  348.      WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
  349.      INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  350.      MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE
  351.      ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS
  352.      WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE
  353.      COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  354.  
  355.  11. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  356.      WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
  357.      MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
  358.      LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
  359.      INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  360.      INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS
  361.      OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  362.      YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH
  363.      ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
  364.      ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  365.  
  366.                       END OF TERMS AND CONDITIONS
  367.  
  368. Appendix: How to Apply These Terms to Your New Programs
  369. =======================================================
  370.  
  371.   If you develop a new program, and you want it to be of the greatest
  372. possible use to humanity, the best way to achieve this is to make it
  373. free software which everyone can redistribute and change under these
  374. terms.
  375.  
  376.   To do so, attach the following notices to the program.  It is safest
  377. to attach them to the start of each source file to most effectively
  378. convey the exclusion of warranty; and each file should have at least
  379. the ``copyright'' line and a pointer to where the full notice is found.
  380.  
  381.      ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
  382.      Copyright (C) 19YY  NAME OF AUTHOR
  383.      
  384.      This program is free software; you can redistribute it and/or modify
  385.      it under the terms of the GNU General Public License as published by
  386.      the Free Software Foundation; either version 1, or (at your option)
  387.      any later version.
  388.      
  389.      This program is distributed in the hope that it will be useful,
  390.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  391.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  392.      GNU General Public License for more details.
  393.      
  394.      You should have received a copy of the GNU General Public License
  395.      along with this program; if not, write to the Free Software
  396.      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  397.  
  398.  Also add information on how to contact you by electronic and paper
  399. mail.
  400.  
  401. If the program is interactive, make it output a short notice like
  402. this when it starts in an interactive mode:
  403.  
  404.      Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR
  405.      Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  406.      This is free software, and you are welcome to redistribute it
  407.      under certain conditions; type `show c' for details.
  408.  
  409.  The hypothetical commands `show w' and `show c' should show the
  410. appropriate parts of the General Public License.  Of course, the
  411. commands you use may be called something other than `show w' and
  412. `show c'; they could even be mouse-clicks or menu items--whatever
  413. suits your program.
  414.  
  415. You should also get your employer (if you work as a programmer) or
  416. your school, if any, to sign a ``copyright disclaimer'' for the
  417. program, if necessary.  Here a sample; alter the names:
  418.  
  419.      Yoyodyne, Inc., hereby disclaims all copyright interest in the
  420.      program `Gnomovision' (a program to direct compilers to make passes
  421.      at assemblers) written by James Hacker.
  422.      
  423.      SIGNATURE OF TY COON, 1 April 1989
  424.      Ty Coon, President of Vice
  425.  
  426. That's all there is to it!
  427.  
  428.  
  429. 
  430. File: gawk-info,  Node: This Manual,  Next: Getting Started,  Prev: License,  Up: Top
  431.  
  432. Using This Manual
  433. *****************
  434.  
  435. The term `gawk' refers to a program (a version of `awk') developed by
  436. the Free Software Foundation, and to the language you use to tell it
  437. what to do.  When we need to be careful, we call the program ``the
  438. `awk' utility'' and the language ``the `awk' language''.  The purpose
  439. of this manual is to explain the `awk' language and how to run the
  440. `awk' utility.
  441.  
  442. The term "`awk' program" refers to a program written by you in the
  443. `awk' programming language.
  444.  
  445. *Note Getting Started::, for the bare essentials you need to know to
  446. start using `awk'.
  447.  
  448. Useful ``one--liners'' are included to give you a feel for the `awk'
  449. language (*note One-liners::.).
  450.  
  451. A sizable sample `awk' program has been provided for you (*note
  452. Sample Program::.).
  453.  
  454. If you find terms that you aren't familiar with, try looking them up
  455. in the glossary (*note Glossary::.).
  456.  
  457. Most of the time complete `awk' programs are used as examples, but in
  458. some of the more advanced sections, only the part of the `awk'
  459. program that illustrates the concept being described is shown.
  460.  
  461. * Menu:
  462.  
  463. This chapter contains the following sections:
  464.  
  465. * The Files::          Sample data files for use in the `awk' programs
  466.                        illustrated in this manual.
  467.  
  468.  
  469. 
  470. File: gawk-info,  Node: The Files,  Up: This Manual
  471.  
  472. Input Files for the Examples
  473. ============================
  474.  
  475. This manual contains many sample programs.  The data for many of
  476. those programs comes from two files.  The first file, called
  477. `BBS-list', represents a list of computer bulletin board systems and
  478. information about those systems.
  479.  
  480. Each line of this file is one "record".  Each record contains the
  481. name of a computer bulletin board, its phone number, the board's baud
  482. rate, and a code for the number of hours it is operational.  An `A'
  483. in the last column means the board operates 24 hours all week.  A `B'
  484. in the last column means the board operates evening and weekend
  485. hours, only.  A `C' means the board operates only on weekends.
  486.  
  487.      aardvark     555-5553     1200/300          B
  488.      alpo-net     555-3412     2400/1200/300     A
  489.      barfly       555-7685     1200/300          A
  490.      bites        555-1675     2400/1200/300     A
  491.      camelot      555-0542     300               C
  492.      core         555-2912     1200/300          C
  493.      fooey        555-1234     2400/1200/300     B
  494.      foot         555-6699     1200/300          B
  495.      macfoo       555-6480     1200/300          A
  496.      sdace        555-3430     2400/1200/300     A
  497.      sabafoo      555-2127     1200/300          C
  498.  
  499. The second data file, called `inventory-shipped', represents
  500. information about shipments during the year.  Each line of this file
  501. is also one record.  Each record contains the month of the year, the
  502. number of green crates shipped, the number of red boxes shipped, the
  503. number of orange bags shipped, and the number of blue packages
  504. shipped, respectively.
  505.  
  506.      Jan  13  25  15 115
  507.      Feb  15  32  24 226
  508.      Mar  15  24  34 228
  509.      Apr  31  52  63 420
  510.      May  16  34  29 208
  511.      Jun  31  42  75 492
  512.      Jul  24  34  67 436
  513.      Aug  15  34  47 316
  514.      Sep  13  55  37 277
  515.      Oct  29  54  68 525
  516.      Nov  20  87  82 577
  517.      Dec  17  35  61 401
  518.      
  519.      Jan  21  36  64 620
  520.      Feb  26  58  80 652
  521.      Mar  24  75  70 495
  522.      Apr  21  70  74 514
  523.  
  524. If you are reading this in GNU Emacs using Info, you can copy the
  525. regions of text showing these sample files into your own test files. 
  526. This way you can try out the examples shown in the remainder of this
  527. document.  You do this by using the command `M-x write-region' to
  528. copy text from the Info file into a file for use with `awk' (see your
  529. ``GNU Emacs Manual'' for more information).  Using this information,
  530. create your own `BBS-list' and `inventory-shipped' files, and
  531. practice what you learn in this manual.
  532.  
  533.  
  534. 
  535. File: gawk-info,  Node: Getting Started,  Next: Reading Files,  Prev: This Manual,  Up: Top
  536.  
  537. Getting Started With `awk'
  538. **************************
  539.  
  540. The basic function of `awk' is to search files for lines (or other
  541. units of text) that contain certain patterns.  When a line matching
  542. any of those patterns is found, `awk' performs specified actions on
  543. that line.  Then `awk' keeps processing input lines until the end of
  544. the file is reached.
  545.  
  546. An `awk' "program" or "script" consists of a series of "rules". 
  547. (They may also contain "function definitions", but that is an
  548. advanced feature, so let's ignore it for now.  *Note User-defined::.)
  549.  
  550. A rule contains a "pattern", an "action", or both.  Actions are
  551. enclosed in curly braces to distinguish them from patterns. 
  552. Therefore, an `awk' program is a sequence of rules in the form:
  553.  
  554.      PATTERN { ACTION }
  555.      PATTERN { ACTION }
  556.      ...
  557.  
  558.  * Menu:
  559.  
  560. * Very Simple::      A very simple example.
  561. * Two Rules::        A less simple one--line example with two rules.
  562. * More Complex::     A more complex example.
  563. * Running gawk::     How to run gawk programs; includes command line syntax.
  564. * Comments::         Adding documentation to gawk programs.
  565. * Statements/Lines:: Subdividing or combining statements into lines.
  566.  
  567. * When::             When to use gawk and when to use other things.
  568.  
  569.  
  570. 
  571. File: gawk-info,  Node: Very Simple,  Next: Two Rules,  Up: Getting Started
  572.  
  573. A Very Simple Example
  574. =====================
  575.  
  576. The following command runs a simple `awk' program that searches the
  577. input file `BBS-list' for the string of characters: `foo'.  (A string
  578. of characters is usually called, quite simply, a "string".)
  579.  
  580.      awk '/foo/ { print $0 }' BBS-list
  581.  
  582. When lines containing `foo' are found, they are printed, because
  583. `print $0' means print the current line.  (Just `print' by itself
  584. also means the same thing, so we could have written that instead.)
  585.  
  586. You will notice that slashes, `/', surround the string `foo' in the
  587. actual `awk' program.  The slashes indicate that `foo' is a pattern
  588. to search for.  This type of pattern is called a "regular
  589. expression", and is covered in more detail later (*note Regexp::.). 
  590. There are single quotes around the `awk' program so that the shell
  591. won't interpret any of it as special shell characters.
  592.  
  593. Here is what this program prints:
  594.  
  595.      fooey        555-1234     2400/1200/300     B
  596.      foot         555-6699     1200/300          B
  597.      macfoo       555-6480     1200/300          A
  598.      sabafoo      555-2127     1200/300          C
  599.  
  600. In an `awk' rule, either the pattern or the action can be omitted,
  601. but not both.
  602.  
  603. If the pattern is omitted, then the action is performed for *every*
  604. input line.
  605.  
  606. If the action is omitted, the default action is to print all lines
  607. that match the pattern.  We could leave out the action (the print
  608. statement and the curly braces) in the above example, and the result
  609. would be the same: all lines matching the pattern `foo' would be
  610. printed.  (By comparison, omitting the print statement but retaining
  611. the curly braces makes an empty action that does nothing; then no
  612. lines would be printed.)
  613.  
  614.  
  615. 
  616. File: gawk-info,  Node: Two Rules,  Next: More Complex,  Prev: Very Simple,  Up: Getting Started
  617.  
  618. An Example with Two Rules
  619. =========================
  620.  
  621. The `awk' utility reads the input files one line at a time.  For each
  622. line, `awk' tries the patterns of all the rules.  If several patterns
  623. match then several actions are run, in the order in which they appear
  624. in the `awk' program.  If no patterns match, then no actions are run.
  625.  
  626. After processing all the rules (perhaps none) that match the line,
  627. `awk' reads the next line (however, *note Next::.).  This continues
  628. until the end of the file is reached.
  629.  
  630. For example, the `awk' program:
  631.  
  632.      /12/  { print $0 }
  633.      /21/  { print $0 }
  634.  
  635. contains two rules.  The first rule has the string `12' as the
  636. pattern and `print $0' as the action.  The second rule has the string
  637. `21' as the pattern and also has `print $0' as the action.  Each
  638. rule's action is enclosed in its own pair of braces.
  639.  
  640. This `awk' program prints every line that contains the string `12'
  641. *or* the string `21'.  If a line contains both strings, it is printed
  642. twice, once by each rule.
  643.  
  644. If we run this program on our two sample data files, `BBS-list' and
  645. `inventory-shipped', as shown here:
  646.  
  647.      awk '/12/ { print $0 }
  648.           /21/ { print $0 }' BBS-list inventory-shipped
  649.  
  650. we get the following output:
  651.  
  652.      aardvark     555-5553     1200/300          B
  653.      alpo-net     555-3412     2400/1200/300     A
  654.      barfly       555-7685     1200/300          A
  655.      bites        555-1675     2400/1200/300     A
  656.      core         555-2912     1200/300          C
  657.      fooey        555-1234     2400/1200/300     B
  658.      foot         555-6699     1200/300          B
  659.      macfoo       555-6480     1200/300          A
  660.      sdace        555-3430     2400/1200/300     A
  661.      sabafoo      555-2127     1200/300          C
  662.      sabafoo      555-2127     1200/300          C
  663.      Jan  21  36  64 620
  664.      Apr  21  70  74 514
  665.  
  666. Note how the line in `BBS-list' beginning with `sabafoo' was printed
  667. twice, once for each rule.
  668.  
  669.  
  670. 
  671. File: gawk-info,  Node: More Complex,  Next: Running gawk,  Prev: Two Rules,  Up: Getting Started
  672.  
  673. A More Complex Example
  674. ======================
  675.  
  676. Here is an example to give you an idea of what typical `awk' programs
  677. do.  This example shows how `awk' can be used to summarize, select,
  678. and rearrange the output of another utility.  It uses features that
  679. haven't been covered yet, so don't worry if you don't understand all
  680. the details.
  681.  
  682.      ls -l | awk '$5 == "Nov" { sum += $4 }
  683.                   END { print sum }'
  684.  
  685. This command prints the total number of bytes in all the files in the
  686. current directory that were last modified in November (of any year). 
  687. (In the C shell you would need to type a semicolon and then a
  688. backslash at the end of the first line; in the Bourne shell you can
  689. type the example as shown.)
  690.  
  691. The `ls -l' part of this example is a command that gives you a full
  692. listing of all the files in a directory, including file size and date.
  693. Its output looks like this:
  694.  
  695.      -rw-r--r--  1 close        1933 Nov  7 13:05 Makefile
  696.      -rw-r--r--  1 close       10809 Nov  7 13:03 gawk.h
  697.      -rw-r--r--  1 close         983 Apr 13 12:14 gawk.tab.h
  698.      -rw-r--r--  1 close       31869 Jun 15 12:20 gawk.y
  699.      -rw-r--r--  1 close       22414 Nov  7 13:03 gawk1.c
  700.      -rw-r--r--  1 close       37455 Nov  7 13:03 gawk2.c
  701.      -rw-r--r--  1 close       27511 Dec  9 13:07 gawk3.c
  702.      -rw-r--r--  1 close        7989 Nov  7 13:03 gawk4.c
  703.  
  704. The first field contains read--write permissions, the second field
  705. contains the number of links to the file, and the third field
  706. identifies the owner of the file.  The fourth field contains the size
  707. of the file in bytes.  The fifth, sixth, and seventh fields contain
  708. the month, day, and time, respectively, that the file was last
  709. modified.  Finally, the eighth field contains the name of the file.
  710.  
  711. The `$5 == "Nov"' in our `awk' program is an expression that tests
  712. whether the fifth field of the output from `ls -l' matches the string
  713. `Nov'.  Each time a line has the string `Nov' in its fifth field, the
  714. action `{ sum += $4 }' is performed.  This adds the fourth field (the
  715. file size) to the variable `sum'.  As a result, when `awk' has
  716. finished reading all the input lines, `sum' will be the sum of the
  717. sizes of files whose lines matched the pattern.
  718.  
  719. After the last line of output from `ls' has been processed, the `END'
  720. pattern is executed, and the value of `sum' is printed.  In this
  721. example, the value of `sum' would be 80600.
  722.  
  723. These more advanced `awk' techniques are covered in later sections
  724. (*note Actions::.).  Before you can move on to more advanced `awk'
  725. programming, you have to know how `awk' interprets your input and
  726. displays your output.  By manipulating "fields" and using special
  727. "print" statements, you can produce some very useful and spectacular
  728. looking reports.
  729.  
  730.  
  731. 
  732. File: gawk-info,  Node: Running gawk,  Next: Comments,  Prev: More Complex,  Up: Getting Started
  733.  
  734. How to Run `awk' Programs
  735. =========================
  736.  
  737. There are several ways to run an `awk' program.  If the program is
  738. short, it is easiest to include it in the command that runs `awk',
  739. like this:
  740.  
  741.      awk 'PROGRAM' INPUT-FILE1 INPUT-FILE2 ...
  742.  
  743.  where PROGRAM consists of a series of PATTERNS and ACTIONS, as
  744. described earlier.
  745.  
  746. When the program is long, you would probably prefer to put it in a
  747. file and run it with a command like this:
  748.  
  749.      awk -f PROGRAM-FILE INPUT-FILE1 INPUT-FILE2 ...
  750.  
  751.  * Menu:
  752.  
  753. * One-shot::      Running a short throw--away `awk' program.
  754. * Read Terminal:: Using no input files (input from terminal instead).
  755. * Long::          Putting permanent `awk' programs in files.
  756. * Executable Scripts:: Making self--contained `awk' programs.
  757. * Command Line::  How the `awk' command line is laid out.
  758.  
  759.  
  760. 
  761. File: gawk-info,  Node: One-shot,  Next: Read Terminal,  Up: Running gawk
  762.  
  763. One--shot Throw--away `awk' Programs
  764. ------------------------------------
  765.  
  766. Once you are familiar with `awk', you will often type simple programs
  767. at the moment you want to use them.  Then you can write the program
  768. as the first argument of the `awk' command, like this:
  769.  
  770.      awk 'PROGRAM' INPUT-FILE1 INPUT-FILE2 ...
  771.  
  772.  where PROGRAM consists of a series of PATTERNS and ACTIONS, as
  773. described earlier.
  774.  
  775. This command format tells the shell to start `awk' and use the
  776. PROGRAM to process records in the input file(s).  There are single
  777. quotes around the PROGRAM so that the shell doesn't interpret any
  778. `awk' characters as special shell characters.  They cause the shell
  779. to treat all of PROGRAM as a single argument for `awk'.  They also
  780. allow PROGRAM to be more than one line long.
  781.  
  782. This format is also useful for running short or medium--sized `awk'
  783. programs from shell scripts, because it avoids the need for a
  784. separate file for the `awk' program.  A self--contained shell script
  785. is more reliable since there are no other files to misplace.
  786.  
  787.  
  788. 
  789. File: gawk-info,  Node: Read Terminal,  Next: Long,  Prev: One-shot,  Up: Running gawk
  790.  
  791. Running `awk' without Input Files
  792. ---------------------------------
  793.  
  794. You can also use `awk' without any input files.  If you type the
  795. command line:
  796.  
  797.      awk 'PROGRAM'
  798.  
  799. then `awk' applies the PROGRAM to the "standard input", which usually
  800. means whatever you type on the terminal.  This continues until you
  801. indicate end--of--file by typing `Control-d'.
  802.  
  803. For example, if you type:
  804.  
  805.      awk '/th/'
  806.  
  807. whatever you type next will be taken as data for that `awk' program. 
  808. If you go on to type the following data,
  809.  
  810.      Kathy
  811.      Ben
  812.      Tom
  813.      Beth
  814.      Seth
  815.      Karen
  816.      Thomas
  817.      `Control-d'
  818.  
  819. then `awk' will print
  820.  
  821.      Kathy
  822.      Beth
  823.      Seth
  824.  
  825. as matching the pattern `th'.  Notice that it did not recognize
  826. `Thomas' as matching the pattern.  The `awk' language is "case
  827. sensitive", and matches patterns *exactly*.
  828.  
  829.  
  830. 
  831. File: gawk-info,  Node: Long,  Next: Executable Scripts,  Prev: Read Terminal,  Up: Running gawk
  832.  
  833. Running Long Programs
  834. ---------------------
  835.  
  836. Sometimes your `awk' programs can be very long.  In this case it is
  837. more convenient to put the program into a separate file.  To tell
  838. `awk' to use that file for its program, you type:
  839.  
  840.      awk -f SOURCE-FILE INPUT-FILE1 INPUT-FILE2 ...
  841.  
  842.  The `-f' tells the `awk' utility to get the `awk' program from the
  843. file SOURCE-FILE.  Any file name can be used for SOURCE-FILE.  For
  844. example, you could put the program:
  845.  
  846.      /th/
  847.  
  848. into the file `th-prog'.  Then the command:
  849.  
  850.      awk -f th-prog
  851.  
  852. does the same thing as this one:
  853.  
  854.      awk '/th/'
  855.  
  856. which was explained earlier (*note Read Terminal::.).  Note that you
  857. don't usually need single quotes around the file name that you
  858. specify with `-f', because most file names don't contain any of the
  859. shell's special characters.
  860.  
  861. If you want to identify your `awk' program files clearly as such, you
  862. can add the extension `.awk' to the filename.  This doesn't affect
  863. the execution of the `awk' program, but it does make ``housekeeping''
  864. easier.
  865.  
  866.  
  867. 
  868. File: gawk-info,  Node: Executable Scripts,  Next: Command Line,  Prev: Long,  Up: Running gawk
  869.  
  870. Executable `awk' Programs
  871. -------------------------
  872.  
  873. (The following section assumes that you are already somewhat familiar
  874. with `awk'.)
  875.  
  876. Once you have learned `awk', you may want to write self--contained
  877. `awk' scripts, using the `#!' script mechanism.  You can do this on
  878. BSD Unix systems and GNU.
  879.  
  880. For example, you could create a text file named `hello', containing
  881. the following (where `BEGIN' is a feature we have not yet discussed):
  882.  
  883.      #! /bin/awk -f
  884.      
  885.      # a sample awk program
  886.      
  887.      BEGIN    { print "hello, world" }
  888.  
  889. After making this file executable (with the `chmod' command), you can
  890. simply type:
  891.  
  892.      hello
  893.  
  894. at the shell, and the system will arrange to run `awk' as if you had
  895. typed:
  896.  
  897.      awk -f hello
  898.  
  899. Self--contained `awk' scripts are particularly useful for putting
  900. `awk' programs into production on your system, without your users
  901. having to know that they are actually using an `awk' program.
  902.  
  903. If your system does not support the `#!' mechanism, you can get a
  904. similar effect using a regular shell script.  It would look something
  905. like this:
  906.  
  907.      : a sample awk program
  908.      
  909.      awk 'PROGRAM' "$@"
  910.  
  911. Using this technique, it is *vital* to enclose the PROGRAM in single
  912. quotes to protect it from interpretation by the shell.  If you omit
  913. the quotes, only a shell wizard can predict the result.
  914.  
  915. The `"$@"' causes the shell to forward all the command line arguments
  916. to the `awk' program, without interpretation.
  917.  
  918.  
  919. 
  920. File: gawk-info,  Node: Command Line,  Prev: Executable Scripts,  Up: Running gawk
  921.  
  922. Details of the `awk' Command Line
  923. ---------------------------------
  924.  
  925. (The following section assumes that you are already familiar with
  926. `awk'.)
  927.  
  928. There are two ways to run `awk'.  Here are templates for both of
  929. them; items enclosed in `[' and `]' in these templates are optional.
  930.  
  931.      awk [ -FFS ] [ -- ] 'PROGRAM' FILE ...
  932.      awk [ -FFS ] -f SOURCE-FILE [ -f SOURCE-FILE ... ] [ -- ] FILE ...
  933.  
  934.  Options begin with a minus sign, and consist of a single character. 
  935. The options and their meanings are as follows:
  936.  
  937. `-FFS'
  938.      This sets the `FS' variable to FS (*note Special::.).  As a
  939.      special case, if FS is `t', then `FS' will be set to the tab
  940.      character (`"\t"').
  941.  
  942. `-f SOURCE-FILE'
  943.      Indicates that the `awk' program is to be found in SOURCE-FILE
  944.      instead of in the first non--option argument.
  945.  
  946. `--'
  947.      This signals the end of the command line options.  If you wish
  948.      to specify an input file named `-f', you can precede it with the
  949.      `--' argument to prevent the `-f' from being interpreted as an
  950.      option.  This handling of `--' follows the POSIX argument
  951.      parsing conventions.
  952.  
  953. Any other options will be flagged as invalid with a warning message,
  954. but are otherwise ignored.
  955.  
  956. If the `-f' option is *not* used, then the first non--option command
  957. line argument is expected to be the program text.
  958.  
  959. The `-f' option may be used more than once on the command line. 
  960. `awk' will read its program source from all of the named files, as if
  961. they had been concatenated together into one big file.  This is
  962. useful for creating libraries of `awk' functions.  Useful functions
  963. can be written once, and then retrieved from a standard place,
  964. instead of having to be included into each individual program.  You
  965. can still type in a program at the terminal and use library
  966. functions, by specifying `/dev/tty' as one of the arguments to a
  967. `-f'.  Type your program, and end it with the keyboard end--of--file
  968. character `Control-d'.
  969.  
  970. Any additional arguments on the command line are made available to
  971. your `awk' program in the `ARGV' array (*note Special::.).  These
  972. arguments are normally treated as input files to be processed in the
  973. order specified.  However, an argument that has the form VAR`='VALUE,
  974. means to assign the value VALUE to the variable VAR--it does not
  975. specify a file at all.
  976.  
  977. Command line options and the program text (if present) are omitted
  978. from the `ARGV' array.  All other arguments, including variable
  979. assignments, are included (*note Special::.).
  980.  
  981. The distinction between file name arguments and variable--assignment
  982. arguments is made when `awk' is about to open the next input file. 
  983. At that point in execution, it checks the ``file name'' to see
  984. whether it is really a variable assignment; if so, instead of trying
  985. to read a file it will, *at that point in the execution*, assign the
  986. variable.
  987.  
  988. Therefore, the variables actually receive the specified values after
  989. all previously specified files have been read.  In particular, the
  990. values of variables assigned in this fashion are *not* available
  991. inside a `BEGIN' rule (*note BEGIN/END::.), since such rules are run
  992. before `awk' begins scanning the argument list.
  993.  
  994. The variable assignment feature is most useful for assigning to
  995. variables such as `RS', `OFS', and `ORS', which control input and
  996. output formats, before listing the data files.  It is also useful for
  997. controlling state if multiple passes are needed over a data file. 
  998. For example:
  999.  
  1000.      awk 'pass == 1  { PASS 1 STUFF }
  1001.           pass == 2  { PASS 2 STUFF }' pass=1 datafile pass=2 datafile
  1002.  
  1003.  
  1004. 
  1005. File: gawk-info,  Node: Comments,  Next: Statements/Lines,  Prev: Running gawk,  Up: Getting Started
  1006.  
  1007. Comments in `awk' Programs
  1008. ==========================
  1009.  
  1010. When you write a complicated `awk' program, you can put "comments" in
  1011. the program file to help you remember what the program does, and how
  1012. it works.
  1013.  
  1014. A comment starts with the the sharp sign character, `#', and
  1015. continues to the end of the line.  The `awk' language ignores the
  1016. rest of a line following a sharp sign.  For example, we could have
  1017. put the following into `th-prog':
  1018.  
  1019.      # This program finds records containing the pattern `th'.  This is how
  1020.      # you continue comments on additional lines.
  1021.      /th/
  1022.  
  1023. You can put comment lines into keyboard--composed throw--away `awk'
  1024. programs also, but this usually isn't very useful; the purpose of a
  1025. comment is to help yourself or another person understand the program
  1026. at another time.
  1027.  
  1028.  
  1029. 
  1030. File: gawk-info,  Node: Statements/Lines,  Next: When,  Prev: Comments,  Up: Getting Started
  1031.  
  1032. `awk' Statements versus Lines
  1033. =============================
  1034.  
  1035. Most often, each line in an `awk' program is a separate statement or
  1036. separate rule, like this:
  1037.  
  1038.      awk '/12/  { print $0 }
  1039.           /21/  { print $0 }' BBS-list inventory-shipped
  1040.  
  1041. But sometimes statements can be more than one line, and lines can
  1042. contain several statements.
  1043.  
  1044. You can split a statement into multiple lines by inserting a newline
  1045. after any of the following:
  1046.  
  1047.      ,    {    ?    :    ||    &&
  1048.  
  1049. Lines ending in `do' or `else' automatically have their statements
  1050. continued on the following line(s).  A newline at any other point
  1051. ends the statement.
  1052.  
  1053. If you would like to split a single statement into two lines at a
  1054. point where a newline would terminate it, you can "continue" it by
  1055. ending the first line with a backslash character, `\'.  This is
  1056. allowed absolutely anywhere in the statement, even in the middle of a
  1057. string or regular expression.  For example:
  1058.  
  1059.      awk '/This program is too long, so continue it\
  1060.       on the next line/ { print $1 }'
  1061.  
  1062. We have generally not used backslash continuation in the sample
  1063. programs in this manual.  Since there is no limit on the length of a
  1064. line, it is never strictly necessary; it just makes programs
  1065. prettier.  We have preferred to make them even more pretty by keeping
  1066. the statements short.  Backslash continuation is most useful when
  1067. your `awk' program is in a separate source file, instead of typed in
  1068. on the command line.
  1069.  
  1070. *Warning: this does not work if you are using the C shell.*
  1071. Continuation with backslash works for `awk' programs in files, and
  1072. also for one--shot programs *provided* you are using the Bourne
  1073. shell, the Korn shell, or the Bourne--again shell.  But the C shell
  1074. used on Berkeley Unix behaves differently!  There, you must use two
  1075. backslashes in a row, followed by a newline.
  1076.  
  1077. When `awk' statements within one rule are short, you might want to
  1078. put more than one of them on a line.  You do this by separating the
  1079. statements with semicolons, `;'.  This also applies to the rules
  1080. themselves.  Thus, the above example program could have been written:
  1081.  
  1082.      /12/ { print $0 } ; /21/ { print $0 }
  1083.  
  1084. *Note:* It is a new requirement that rules on the same line require
  1085. semicolons as a separator in the `awk' language; it was done for
  1086. consistency with the statements in the action part of rules.
  1087.  
  1088.  
  1089. 
  1090. File: gawk-info,  Node: When,  Prev: Statements/Lines,  Up: Getting Started
  1091.  
  1092. When to Use `awk'
  1093. =================
  1094.  
  1095. What use is all of this to me, you might ask?  Using additional
  1096. operating system utilities, more advanced patterns, field separators,
  1097. arithmetic statements, and other selection criteria, you can produce
  1098. much more complex output.  The `awk' language is very useful for
  1099. producing reports from large amounts of raw data, like summarizing
  1100. information from the output of standard operating system programs
  1101. such as `ls'.  (*Note A More Complex Example: More Complex.)
  1102.  
  1103. Programs written with `awk' are usually much smaller than they would
  1104. be in other languages.  This makes `awk' programs easy to compose and
  1105. use.  Often `awk' programs can be quickly composed at your terminal,
  1106. used once, and thrown away.  Since `awk' programs are interpreted,
  1107. you can avoid the usually lengthy edit--compile--test--debug cycle of
  1108. software development.
  1109.  
  1110. Complex programs have been written in `awk', including a complete
  1111. retargetable assembler for 8--bit microprocessors (*note Glossary::.
  1112. for more information) and a microcode assembler for a special purpose
  1113. Prolog computer.  However, `awk''s capabilities are strained by tasks
  1114. of such complexity.
  1115.  
  1116. If you find yourself writing `awk' scripts of more than, say, a few
  1117. hundred lines, you might consider using a different programming
  1118. language.  Emacs Lisp is a good choice if you need sophisticated
  1119. string or pattern matching capabilities.  The shell is also good at
  1120. string and pattern matching; in addition it allows powerful use of
  1121. the standard utilities.  More conventional languages like C, C++, or
  1122. Lisp offer better facilities for system programming and for managing
  1123. the complexity of large programs.  Programs in these languages may
  1124. require more lines of source code than the equivalent `awk' programs,
  1125. but they will be easier to maintain and usually run more efficiently.
  1126.  
  1127.  
  1128. 
  1129. File: gawk-info,  Node: Reading Files,  Next: Printing,  Prev: Getting Started,  Up: Top
  1130.  
  1131. Reading Files (Input)
  1132. *********************
  1133.  
  1134. In the typical `awk' program, all input is read either from the
  1135. standard input (usually the keyboard) or from files whose names you
  1136. specify on the `awk' command line.  If you specify input files, `awk'
  1137. reads data from the first one until it reaches the end; then it reads
  1138. the second file until it reaches the end, and so on.  The name of the
  1139. current input file can be found in the special variable `FILENAME'
  1140. (*note Special::.).
  1141.  
  1142. The input is split automatically into "records", and processed by the
  1143. rules one record at a time.  (Records are the units of text mentioned
  1144. in the introduction; by default, a record is a line of text.) Each
  1145. record read is split automatically into "fields", to make it more
  1146. convenient for a rule to work on parts of the record under
  1147. consideration.
  1148.  
  1149. On rare occasions you will need to use the `getline' command, which
  1150. can do explicit input from any number of files.
  1151.  
  1152. * Menu:
  1153.  
  1154. * Records::            Controlling how data is split into records.
  1155. * Fields::             An introduction to fields.
  1156. * Field Separators::   The field separator and how to change it.
  1157. * Multiple::           Reading multi--line records.
  1158.  
  1159. * Assignment Options:: Setting variables on the command line and a summary
  1160.                        of command line syntax.  This is an advanced method
  1161.                        of input.
  1162.  
  1163. * Getline::            Reading files under explicit program control
  1164.                        using the `getline' function.
  1165. * Close Input::        Closing an input file (so you can read from
  1166.                        the beginning once more).
  1167.  
  1168.  
  1169. 
  1170. File: gawk-info,  Node: Records,  Next: Fields,  Up: Reading Files
  1171.  
  1172. How Input is Split into Records
  1173. ===============================
  1174.  
  1175. The `awk' language divides its input into records and fields. 
  1176. Records are separated from each other by the "record separator".  By
  1177. default, the record separator is the "newline" character.  Therefore,
  1178. normally, a record is a line of text.
  1179.  
  1180. Sometimes you may want to use a different character to separate your
  1181. records.  You can use different characters by changing the special
  1182. variable `RS'.
  1183.  
  1184. The value of `RS' is a string that says how to separate records; the
  1185. default value is `"\n"', the string of just a newline character. 
  1186. This is why lines of text are the default record.  Although `RS' can
  1187. have any string as its value, only the first character of the string
  1188. will be used as the record separator.  The other characters are
  1189. ignored.  `RS' is exceptional in this regard; `awk' uses the full
  1190. value of all its other special variables.
  1191.  
  1192. The value of `RS' is changed by "assigning" it a new value (*note
  1193. Assignment Ops::.).  One way to do this is at the beginning of your
  1194. `awk' program, before any input has been processed, using the special
  1195. `BEGIN' pattern (*note BEGIN/END::.).  This way, `RS' is changed to
  1196. its new value before any input is read.  The new value of `RS' is
  1197. enclosed in quotation marks.  For example:
  1198.  
  1199.      awk 'BEGIN { RS = "/" } ; { print $0 }' BBS-list
  1200.  
  1201. changes the value of `RS' to `/', the slash character, before reading
  1202. any input.  Records are now separated by a slash.  The second rule in
  1203. the `awk' program (the action with no pattern) will proceed to print
  1204. each record.  Since each `print' statement adds a newline at the end
  1205. of its output, the effect of this `awk' program is to copy the input
  1206. with each slash changed to a newline.
  1207.  
  1208. Another way to change the record separator is on the command line,
  1209. using the variable--assignment feature (*note Command Line::.).
  1210.  
  1211.      awk '...' RS="/" SOURCE-FILE
  1212.  
  1213. `RS' will be set to `/' before processing SOURCE-FILE.
  1214.  
  1215. The empty string (a string of no characters) has a special meaning as
  1216. the value of `RS': it means that records are separated only by blank
  1217. lines.  *Note Multiple::, for more details.
  1218.  
  1219. The `awk' utility keeps track of the number of records that have been
  1220. read so far from the current input file.  This value is stored in a
  1221. special variable called `FNR'.  It is reset to zero when a new file
  1222. is started.  Another variable, `NR', is the total number of input
  1223. records read so far from all files.  It starts at zero but is never
  1224. automatically reset to zero.
  1225.  
  1226. If you change the value of `RS' in the middle of an `awk' run, the
  1227. new value is used to delimit subsequent records, but the record
  1228. currently being processed (and records already finished) are not
  1229. affected.
  1230.  
  1231.  
  1232.