home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / bbs / gnu / gawk-2.15.5-src.lha / GNU / src / amiga / gawk-2.15.5 / gawk.info-1 < prev    next >
Encoding:
GNU Info File  |  1994-06-13  |  47.1 KB  |  1,070 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.  
  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: Top,  Next: Preface,  Prev: (dir),  Up: (dir)
  29.  
  30. General Introduction
  31. ********************
  32.  
  33.    This file documents `awk', a program that you can use to select
  34. particular records in a file and perform operations upon them.
  35.  
  36.    This is Edition 0.15 of `The GAWK Manual',
  37. for the 2.15 version of the GNU implementation
  38. of AWK.
  39.  
  40. * Menu:
  41.  
  42. * Preface::                     What you can do with `awk'; brief history
  43.                                 and acknowledgements.
  44. * Copying::                     Your right to copy and distribute `gawk'.
  45. * This Manual::                 Using this manual.
  46.                                 Includes sample input files that you can use.
  47. * Getting Started::             A basic introduction to using `awk'.
  48.                                 How to run an `awk' program.
  49.                                 Command line syntax.
  50. * Reading Files::               How to read files and manipulate fields.
  51. * Printing::                    How to print using `awk'.  Describes the
  52.                                 `print' and `printf' statements.
  53.                                 Also describes redirection of output.
  54. * One-liners::                  Short, sample `awk' programs.
  55. * Patterns::                    The various types of patterns
  56.                                 explained in detail.
  57. * Actions::                     The various types of actions are
  58.                                 introduced here.  Describes
  59.                                 expressions and the various operators in
  60.                                 detail.  Also describes comparison expressions.
  61. * Expressions::                 Expressions are the basic building
  62.                                 blocks of statements.
  63. * Statements::                  The various control statements are
  64.                                 described in detail.
  65. * Arrays::                      The description and use of arrays.
  66.                                 Also includes array-oriented control
  67.                                 statements.
  68. * Built-in::                    The built-in functions are summarized here.
  69. * User-defined::                User-defined functions are described in detail.
  70. * Built-in Variables::          Built-in Variables
  71. * Command Line::                How to run `gawk'.
  72. * Language History::            The evolution of the `awk' language.
  73. * Installation::                Installing `gawk' under
  74.                                 various operating systems.
  75. * Gawk Summary::                `gawk' Options and Language Summary.
  76. * Sample Program::              A sample `awk' program with a
  77.                                 complete explanation.
  78. * Bugs::                        Reporting Problems and Bugs.
  79. * Notes::                       Something about the
  80.                                 implementation of `gawk'.
  81. * Glossary::                    An explanation of some unfamiliar terms.
  82. * Index::
  83.  
  84. 
  85. File: gawk.info,  Node: Preface,  Next: Copying,  Prev: Top,  Up: Top
  86.  
  87. Preface
  88. *******
  89.  
  90.    If you are like many computer users, you would frequently like to
  91. make changes in various text files wherever certain patterns appear, or
  92. extract data from parts of certain lines while discarding the rest.  To
  93. write a program to do this in a language such as C or Pascal is a
  94. time-consuming inconvenience that may take many lines of code.  The job
  95. may be easier with `awk'.
  96.  
  97.    The `awk' utility interprets a special-purpose programming language
  98. that makes it possible to handle simple data-reformatting jobs easily
  99. with just a few lines of code.
  100.  
  101.    The GNU implementation of `awk' is called `gawk'; it is fully upward
  102. compatible with the System V Release 4 version of `awk'.  `gawk' is
  103. also upward compatible with the POSIX (draft) specification of the
  104. `awk' language.  This means that all properly written `awk' programs
  105. should work with `gawk'.  Thus, we usually don't distinguish between
  106. `gawk' and other `awk' implementations in this manual.
  107.  
  108.    This manual teaches you what `awk' does and how you can use `awk'
  109. effectively.  You should already be familiar with basic system commands
  110. such as `ls'.  Using `awk' you can:
  111.  
  112.    * manage small, personal databases
  113.  
  114.    * generate reports
  115.  
  116.    * validate data
  117.  
  118.    * produce indexes, and perform other document preparation tasks
  119.  
  120.    * even experiment with algorithms that can be adapted later to other
  121.      computer languages
  122.  
  123. * Menu:
  124.  
  125. * History::                     The history of `gawk' and
  126.                                 `awk'.  Acknowledgements.
  127.  
  128. 
  129. File: gawk.info,  Node: History,  Prev: Preface,  Up: Preface
  130.  
  131. History of `awk' and `gawk'
  132. ===========================
  133.  
  134.    The name `awk' comes from the initials of its designers: Alfred V.
  135. Aho, Peter J. Weinberger, and Brian W. Kernighan.  The original version
  136. of `awk' was written in 1977.  In 1985 a new version made the
  137. programming language more powerful, introducing user-defined functions,
  138. multiple input streams, and computed regular expressions.  This new
  139. version became generally available with System V Release 3.1.  The
  140. version in System V Release 4 added some new features and also cleaned
  141. up the behavior in some of the "dark corners" of the language.  The
  142. specification for `awk' in the POSIX Command Language and Utilities
  143. standard further clarified the language based on feedback from both the
  144. `gawk' designers, and the original `awk' designers.
  145.  
  146.    The GNU implementation, `gawk', was written in 1986 by Paul Rubin
  147. and Jay Fenlason, with advice from Richard Stallman.  John Woods
  148. contributed parts of the code as well.  In 1988 and 1989, David
  149. Trueman, with help from Arnold Robbins, thoroughly reworked `gawk' for
  150. compatibility with the newer `awk'.  Current development (1992) focuses
  151. on bug fixes, performance improvements, and standards compliance.
  152.  
  153.    We need to thank many people for their assistance in producing this
  154. manual.  Jay Fenlason contributed many ideas and sample programs.
  155. Richard Mlynarik and Robert J. Chassell gave helpful comments on early
  156. drafts of this manual.  The paper `A Supplemental Document for `awk''
  157. by John W.  Pierce of the Chemistry Department at UC San Diego,
  158. pinpointed several issues relevant both to `awk' implementation and to
  159. this manual, that would otherwise have escaped us.  David Trueman, Pat
  160. Rankin, and Michal Jaegermann also contributed sections of the manual.
  161.  
  162.    The following people provided many helpful comments on this edition
  163. of the manual: Rick Adams, Michael Brennan, Rich Burridge, Diane Close,
  164. Christopher ("Topher") Eliot, Michael Lijewski, Pat Rankin, Miriam
  165. Robbins, and Michal Jaegermann.  Robert J. Chassell provided much
  166. valuable advice on the use of Texinfo.
  167.  
  168.    Finally, we would like to thank Brian Kernighan of Bell Labs for
  169. invaluable assistance during the testing and debugging of `gawk', and
  170. for help in clarifying numerous points about the language.
  171.  
  172. 
  173. File: gawk.info,  Node: Copying,  Next: This Manual,  Prev: Preface,  Up: Top
  174.  
  175. GNU GENERAL PUBLIC LICENSE
  176. **************************
  177.  
  178.                          Version 2, June 1991
  179.  
  180.      Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  181.      675 Mass Ave, Cambridge, MA 02139, USA
  182.      
  183.      Everyone is permitted to copy and distribute verbatim copies
  184.      of this license document, but changing it is not allowed.
  185.  
  186. Preamble
  187. ========
  188.  
  189.    The licenses for most software are designed to take away your
  190. freedom to share and change it.  By contrast, the GNU General Public
  191. License is intended to guarantee your freedom to share and change free
  192. software--to make sure the software is free for all its users.  This
  193. General Public License applies to most of the Free Software
  194. Foundation's software and to any other program whose authors commit to
  195. using it.  (Some other Free Software Foundation software is covered by
  196. the GNU Library General Public License instead.)  You can apply it to
  197. your programs, too.
  198.  
  199.    When we speak of free software, we are referring to freedom, not
  200. price.  Our General Public Licenses are designed to make sure that you
  201. have the freedom to distribute copies of free software (and charge for
  202. this service if you wish), that you receive source code or can get it
  203. if you want it, that you can change the software or use pieces of it in
  204. new free programs; and that you know you can do these things.
  205.  
  206.    To protect your rights, we need to make restrictions that forbid
  207. anyone to deny you these rights or to ask you to surrender the rights.
  208. These restrictions translate to certain responsibilities for you if you
  209. distribute copies of the software, or if you modify it.
  210.  
  211.    For example, if you distribute copies of such a program, whether
  212. gratis or for a fee, you must give the recipients all the rights that
  213. you have.  You must make sure that they, too, receive or can get the
  214. source code.  And you must show them these terms so they know their
  215. rights.
  216.  
  217.    We protect your rights with two steps: (1) copyright the software,
  218. and (2) offer you this license which gives you legal permission to copy,
  219. distribute and/or modify the software.
  220.  
  221.    Also, for each author's protection and ours, we want to make certain
  222. that everyone understands that there is no warranty for this free
  223. software.  If the software is modified by someone else and passed on, we
  224. want its recipients to know that what they have is not the original, so
  225. that any problems introduced by others will not reflect on the original
  226. authors' reputations.
  227.  
  228.    Finally, any free program is threatened constantly by software
  229. patents.  We wish to avoid the danger that redistributors of a free
  230. program will individually obtain patent licenses, in effect making the
  231. program proprietary.  To prevent this, we have made it clear that any
  232. patent must be licensed for everyone's free use or not licensed at all.
  233.  
  234.    The precise terms and conditions for copying, distribution and
  235. modification follow.
  236.  
  237.     TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  238.  
  239.   1. This License applies to any program or other work which contains a
  240.      notice placed by the copyright holder saying it may be distributed
  241.      under the terms of this General Public License.  The "Program",
  242.      below, refers to any such program or work, and a "work based on
  243.      the Program" means either the Program or any derivative work under
  244.      copyright law: that is to say, a work containing the Program or a
  245.      portion of it, either verbatim or with modifications and/or
  246.      translated into another language.  (Hereinafter, translation is
  247.      included without limitation in the term "modification".)  Each
  248.      licensee is addressed as "you".
  249.  
  250.      Activities other than copying, distribution and modification are
  251.      not covered by this License; they are outside its scope.  The act
  252.      of running the Program is not restricted, and the output from the
  253.      Program is covered only if its contents constitute a work based on
  254.      the Program (independent of having been made by running the
  255.      Program).  Whether that is true depends on what the Program does.
  256.  
  257.   2. You may copy and distribute verbatim copies of the Program's
  258.      source code as you receive it, in any medium, provided that you
  259.      conspicuously and appropriately publish on each copy an appropriate
  260.      copyright notice and disclaimer of warranty; keep intact all the
  261.      notices that refer to this License and to the absence of any
  262.      warranty; and give any other recipients of the Program a copy of
  263.      this License along with the Program.
  264.  
  265.      You may charge a fee for the physical act of transferring a copy,
  266.      and you may at your option offer warranty protection in exchange
  267.      for a fee.
  268.  
  269.   3. You may modify your copy or copies of the Program or any portion
  270.      of it, thus forming a work based on the Program, and copy and
  271.      distribute such modifications or work under the terms of Section 1
  272.      above, provided that you also meet all of these conditions:
  273.  
  274.        a. You must cause the modified files to carry prominent notices
  275.           stating that you changed the files and the date of any change.
  276.  
  277.        b. You must cause any work that you distribute or publish, that
  278.           in whole or in part contains or is derived from the Program
  279.           or any part thereof, to be licensed as a whole at no charge
  280.           to all third parties under the terms of this License.
  281.  
  282.        c. If the modified program normally reads commands interactively
  283.           when run, you must cause it, when started running for such
  284.           interactive use in the most ordinary way, to print or display
  285.           an announcement including an appropriate copyright notice and
  286.           a notice that there is no warranty (or else, saying that you
  287.           provide a warranty) and that users may redistribute the
  288.           program under these conditions, and telling the user how to
  289.           view a copy of this License.  (Exception: if the Program
  290.           itself is interactive but does not normally print such an
  291.           announcement, your work based on the Program is not required
  292.           to print an announcement.)
  293.  
  294.      These requirements apply to the modified work as a whole.  If
  295.      identifiable sections of that work are not derived from the
  296.      Program, and can be reasonably considered independent and separate
  297.      works in themselves, then this License, and its terms, do not
  298.      apply to those sections when you distribute them as separate
  299.      works.  But when you distribute the same sections as part of a
  300.      whole which is a work based on the Program, the distribution of
  301.      the whole must be on the terms of this License, whose permissions
  302.      for other licensees extend to the entire whole, and thus to each
  303.      and every part regardless of who wrote it.
  304.  
  305.      Thus, it is not the intent of this section to claim rights or
  306.      contest your rights to work written entirely by you; rather, the
  307.      intent is to exercise the right to control the distribution of
  308.      derivative or collective works based on the Program.
  309.  
  310.      In addition, mere aggregation of another work not based on the
  311.      Program with the Program (or with a work based on the Program) on
  312.      a volume of a storage or distribution medium does not bring the
  313.      other work under the scope of this License.
  314.  
  315.   4. You may copy and distribute the Program (or a work based on it,
  316.      under Section 2) in object code or executable form under the terms
  317.      of Sections 1 and 2 above provided that you also do one of the
  318.      following:
  319.  
  320.        a. Accompany it with the complete corresponding machine-readable
  321.           source code, which must be distributed under the terms of
  322.           Sections 1 and 2 above on a medium customarily used for
  323.           software interchange; or,
  324.  
  325.        b. Accompany it with a written offer, valid for at least three
  326.           years, to give any third party, for a charge no more than your
  327.           cost of physically performing source distribution, a complete
  328.           machine-readable copy of the corresponding source code, to be
  329.           distributed under the terms of Sections 1 and 2 above on a
  330.           medium customarily used for software interchange; or,
  331.  
  332.        c. Accompany it with the information you received as to the offer
  333.           to distribute corresponding source code.  (This alternative is
  334.           allowed only for noncommercial distribution and only if you
  335.           received the program in object code or executable form with
  336.           such an offer, in accord with Subsection b above.)
  337.  
  338.      The source code for a work means the preferred form of the work for
  339.      making modifications to it.  For an executable work, complete
  340.      source code means all the source code for all modules it contains,
  341.      plus any associated interface definition files, plus the scripts
  342.      used to control compilation and installation of the executable.
  343.      However, as a special exception, the source code distributed need
  344.      not include anything that is normally distributed (in either
  345.      source or binary form) with the major components (compiler,
  346.      kernel, and so on) of the operating system on which the executable
  347.      runs, unless that component itself accompanies the executable.
  348.  
  349.      If distribution of executable or object code is made by offering
  350.      access to copy from a designated place, then offering equivalent
  351.      access to copy the source code from the same place counts as
  352.      distribution of the source code, even though third parties are not
  353.      compelled to copy the source along with the object code.
  354.  
  355.   5. You may not copy, modify, sublicense, or distribute the Program
  356.      except as expressly provided under this License.  Any attempt
  357.      otherwise to copy, modify, sublicense or distribute the Program is
  358.      void, and will automatically terminate your rights under this
  359.      License.  However, parties who have received copies, or rights,
  360.      from you under this License will not have their licenses
  361.      terminated so long as such parties remain in full compliance.
  362.  
  363.   6. You are not required to accept this License, since you have not
  364.      signed it.  However, nothing else grants you permission to modify
  365.      or distribute the Program or its derivative works.  These actions
  366.      are prohibited by law if you do not accept this License.
  367.      Therefore, by modifying or distributing the Program (or any work
  368.      based on the Program), you indicate your acceptance of this
  369.      License to do so, and all its terms and conditions for copying,
  370.      distributing or modifying the Program or works based on it.
  371.  
  372.   7. Each time you redistribute the Program (or any work based on the
  373.      Program), the recipient automatically receives a license from the
  374.      original licensor to copy, distribute or modify the Program
  375.      subject to these terms and conditions.  You may not impose any
  376.      further restrictions on the recipients' exercise of the rights
  377.      granted herein.  You are not responsible for enforcing compliance
  378.      by third parties to this License.
  379.  
  380.   8. If, as a consequence of a court judgment or allegation of patent
  381.      infringement or for any other reason (not limited to patent
  382.      issues), conditions are imposed on you (whether by court order,
  383.      agreement or otherwise) that contradict the conditions of this
  384.      License, they do not excuse you from the conditions of this
  385.      License.  If you cannot distribute so as to satisfy simultaneously
  386.      your obligations under this License and any other pertinent
  387.      obligations, then as a consequence you may not distribute the
  388.      Program at all.  For example, if a patent license would not permit
  389.      royalty-free redistribution of the Program by all those who
  390.      receive copies directly or indirectly through you, then the only
  391.      way you could satisfy both it and this License would be to refrain
  392.      entirely from distribution of the Program.
  393.  
  394.      If any portion of this section is held invalid or unenforceable
  395.      under any particular circumstance, the balance of the section is
  396.      intended to apply and the section as a whole is intended to apply
  397.      in other circumstances.
  398.  
  399.      It is not the purpose of this section to induce you to infringe any
  400.      patents or other property right claims or to contest validity of
  401.      any such claims; this section has the sole purpose of protecting
  402.      the integrity of the free software distribution system, which is
  403.      implemented by public license practices.  Many people have made
  404.      generous contributions to the wide range of software distributed
  405.      through that system in reliance on consistent application of that
  406.      system; it is up to the author/donor to decide if he or she is
  407.      willing to distribute software through any other system and a
  408.      licensee cannot impose that choice.
  409.  
  410.      This section is intended to make thoroughly clear what is believed
  411.      to be a consequence of the rest of this License.
  412.  
  413.   9. If the distribution and/or use of the Program is restricted in
  414.      certain countries either by patents or by copyrighted interfaces,
  415.      the original copyright holder who places the Program under this
  416.      License may add an explicit geographical distribution limitation
  417.      excluding those countries, so that distribution is permitted only
  418.      in or among countries not thus excluded.  In such case, this
  419.      License incorporates the limitation as if written in the body of
  420.      this License.
  421.  
  422.  10. The Free Software Foundation may publish revised and/or new
  423.      versions of the General Public License from time to time.  Such
  424.      new versions will be similar in spirit to the present version, but
  425.      may differ in detail to address new problems or concerns.
  426.  
  427.      Each version is given a distinguishing version number.  If the
  428.      Program specifies a version number of this License which applies
  429.      to it and "any later version", you have the option of following
  430.      the terms and conditions either of that version or of any later
  431.      version published by the Free Software Foundation.  If the Program
  432.      does not specify a version number of this License, you may choose
  433.      any version ever published by the Free Software Foundation.
  434.  
  435.  11. If you wish to incorporate parts of the Program into other free
  436.      programs whose distribution conditions are different, write to the
  437.      author to ask for permission.  For software which is copyrighted
  438.      by the Free Software Foundation, write to the Free Software
  439.      Foundation; we sometimes make exceptions for this.  Our decision
  440.      will be guided by the two goals of preserving the free status of
  441.      all derivatives of our free software and of promoting the sharing
  442.      and reuse of software generally.
  443.  
  444.                                 NO WARRANTY
  445.  
  446.  12. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
  447.      WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
  448.      LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  449.      HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
  450.      WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
  451.      NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  452.      FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE
  453.      QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  454.      PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  455.      SERVICING, REPAIR OR CORRECTION.
  456.  
  457.  13. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  458.      WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
  459.      MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
  460.      LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
  461.      INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  462.      INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
  463.      DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU
  464.      OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
  465.      OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
  466.      ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  467.  
  468.                       END OF TERMS AND CONDITIONS
  469.  
  470. How to Apply These Terms to Your New Programs
  471. =============================================
  472.  
  473.    If you develop a new program, and you want it to be of the greatest
  474. possible use to the public, the best way to achieve this is to make it
  475. free software which everyone can redistribute and change under these
  476. terms.
  477.  
  478.    To do so, attach the following notices to the program.  It is safest
  479. to attach them to the start of each source file to most effectively
  480. convey the exclusion of warranty; and each file should have at least
  481. the "copyright" line and a pointer to where the full notice is found.
  482.  
  483.      ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
  484.      Copyright (C) 19YY  NAME OF AUTHOR
  485.      
  486.      This program is free software; you can redistribute it and/or modify
  487.      it under the terms of the GNU General Public License as published by
  488.      the Free Software Foundation; either version 2 of the License, or
  489.      (at your option) any later version.
  490.      
  491.      This program is distributed in the hope that it will be useful,
  492.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  493.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  494.      GNU General Public License for more details.
  495.      
  496.      You should have received a copy of the GNU General Public License
  497.      along with this program; if not, write to the Free Software
  498.      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  499.  
  500.    Also add information on how to contact you by electronic and paper
  501. mail.
  502.  
  503.    If the program is interactive, make it output a short notice like
  504. this when it starts in an interactive mode:
  505.  
  506.      Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR
  507.      Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
  508.      type `show w'.
  509.      This is free software, and you are welcome to redistribute it
  510.      under certain conditions; type `show c' for details.
  511.  
  512.    The hypothetical commands `show w' and `show c' should show the
  513. appropriate parts of the General Public License.  Of course, the
  514. commands you use may be called something other than `show w' and `show
  515. c'; they could even be mouse-clicks or menu items--whatever suits your
  516. program.
  517.  
  518.    You should also get your employer (if you work as a programmer) or
  519. your school, if any, to sign a "copyright disclaimer" for the program,
  520. if necessary.  Here is a sample; alter the names:
  521.  
  522.      Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  523.      `Gnomovision' (which makes passes at compilers) written by James Hacker.
  524.      
  525.      SIGNATURE OF TY COON, 1 April 1989
  526.      Ty Coon, President of Vice
  527.  
  528.    This General Public License does not permit incorporating your
  529. program into proprietary programs.  If your program is a subroutine
  530. library, you may consider it more useful to permit linking proprietary
  531. applications with the library.  If this is what you want to do, use the
  532. GNU Library General Public License instead of this License.
  533.  
  534. 
  535. File: gawk.info,  Node: This Manual,  Next: Getting Started,  Prev: Copying,  Up: Top
  536.  
  537. Using this Manual
  538. *****************
  539.  
  540.    The term `awk' refers to a particular program, and to the language
  541. you use to tell this program what to do.  When we need to be careful,
  542. we call the program "the `awk' utility" and the language "the `awk'
  543. language."  The term `gawk' refers to a version of `awk' developed as
  544. part the GNU project.  The purpose of this manual is to explain both the
  545. `awk' language and how to run the `awk' utility.
  546.  
  547.    While concentrating on the features of `gawk', the manual will also
  548. attempt to describe important differences between `gawk' and other
  549. `awk' implementations.  In particular, any features that are not in the
  550. POSIX standard for `awk' will be noted.
  551.  
  552.    The term "`awk' program" refers to a program written by you in the
  553. `awk' programming language.
  554.  
  555.    *Note Getting Started with `awk': Getting Started, for the bare
  556. essentials you need to know to start using `awk'.
  557.  
  558.    Some useful "one-liners" are included to give you a feel for the
  559. `awk' language (*note Useful "One-liners": One-liners.).
  560.  
  561.    A sample `awk' program has been provided for you (*note Sample
  562. Program::.).
  563.  
  564.    If you find terms that you aren't familiar with, try looking them up
  565. in the glossary (*note Glossary::.).
  566.  
  567.    The entire `awk' language is summarized for quick reference in *Note
  568. `gawk' Summary: Gawk Summary.  Look there if you just need to refresh
  569. your memory about a particular feature.
  570.  
  571.    Most of the time complete `awk' programs are used as examples, but in
  572. some of the more advanced sections, only the part of the `awk' program
  573. that illustrates the concept being described is shown.
  574.  
  575. * Menu:
  576.  
  577. * Sample Data Files::           Sample data files for use in the `awk'
  578.                                 programs illustrated in this manual.
  579.  
  580. 
  581. File: gawk.info,  Node: Sample Data Files,  Prev: This Manual,  Up: This Manual
  582.  
  583. Data Files for the Examples
  584. ===========================
  585.  
  586.    Many of the examples in this manual take their input from two sample
  587. data files.  The first, called `BBS-list', represents a list of
  588. computer bulletin board systems together with information about those
  589. systems.  The second data file, called `inventory-shipped', contains
  590. information about shipments on a monthly basis.  Each line of these
  591. files is one "record".
  592.  
  593.    In the file `BBS-list', each record contains the name of a computer
  594. bulletin board, its phone number, the board's baud rate, and a code for
  595. the number of hours it is operational.  An `A' in the last column means
  596. the board operates 24 hours a day.  A `B' in the last column means the
  597. board operates evening and weekend hours, only.  A `C' means the board
  598. operates only on weekends.
  599.  
  600.      aardvark     555-5553     1200/300          B
  601.      alpo-net     555-3412     2400/1200/300     A
  602.      barfly       555-7685     1200/300          A
  603.      bites        555-1675     2400/1200/300     A
  604.      camelot      555-0542     300               C
  605.      core         555-2912     1200/300          C
  606.      fooey        555-1234     2400/1200/300     B
  607.      foot         555-6699     1200/300          B
  608.      macfoo       555-6480     1200/300          A
  609.      sdace        555-3430     2400/1200/300     A
  610.      sabafoo      555-2127     1200/300          C
  611.  
  612.    The second data file, called `inventory-shipped', represents
  613. information about shipments during the year.  Each record contains the
  614. month of the year, the number of green crates shipped, the number of
  615. red boxes shipped, the number of orange bags shipped, and the number of
  616. blue packages shipped, respectively.  There are 16 entries, covering
  617. the 12 months of one year and 4 months of the next year.
  618.  
  619.      Jan  13  25  15 115
  620.      Feb  15  32  24 226
  621.      Mar  15  24  34 228
  622.      Apr  31  52  63 420
  623.      May  16  34  29 208
  624.      Jun  31  42  75 492
  625.      Jul  24  34  67 436
  626.      Aug  15  34  47 316
  627.      Sep  13  55  37 277
  628.      Oct  29  54  68 525
  629.      Nov  20  87  82 577
  630.      Dec  17  35  61 401
  631.      
  632.      Jan  21  36  64 620
  633.      Feb  26  58  80 652
  634.      Mar  24  75  70 495
  635.      Apr  21  70  74 514
  636.  
  637.    If you are reading this in GNU Emacs using Info, you can copy the
  638. regions of text showing these sample files into your own test files.
  639. This way you can try out the examples shown in the remainder of this
  640. document.  You do this by using the command `M-x write-region' to copy
  641. text from the Info file into a file for use with `awk' (*Note Misc File
  642. Ops: (emacs)Misc File Ops, for more information).  Using this
  643. information, create your own `BBS-list' and `inventory-shipped' files,
  644. and practice what you learn in this manual.
  645.  
  646. 
  647. File: gawk.info,  Node: Getting Started,  Next: Reading Files,  Prev: This Manual,  Up: Top
  648.  
  649. Getting Started with `awk'
  650. **************************
  651.  
  652.    The basic function of `awk' is to search files for lines (or other
  653. units of text) that contain certain patterns.  When a line matches one
  654. of the patterns, `awk' performs specified actions on that line.  `awk'
  655. keeps processing input lines in this way until the end of the input
  656. file is reached.
  657.  
  658.    When you run `awk', you specify an `awk' "program" which tells `awk'
  659. what to do.  The program consists of a series of "rules".  (It may also
  660. contain "function definitions", but that is an advanced feature, so we
  661. will ignore it for now.  *Note User-defined Functions: User-defined.)
  662. Each rule specifies one pattern to search for, and one action to
  663. perform when that pattern is found.
  664.  
  665.    Syntactically, a rule consists of a pattern followed by an action.
  666. The action is enclosed in curly braces to separate it from the pattern.
  667. Rules are usually separated by newlines.  Therefore, an `awk' program
  668. looks like this:
  669.  
  670.      PATTERN { ACTION }
  671.      PATTERN { ACTION }
  672.      ...
  673.  
  674. * Menu:
  675.  
  676. * Very Simple::                 A very simple example.
  677. * Two Rules::                   A less simple one-line example with two rules.
  678. * More Complex::                A more complex example.
  679. * Running gawk::                How to run `gawk' programs;
  680.                                 includes command line syntax.
  681. * Comments::                    Adding documentation to `gawk' programs.
  682. * Statements/Lines::            Subdividing or combining statements into lines.
  683. * When::                        When to use `gawk' and
  684.                                 when to use other things.
  685.  
  686. 
  687. File: gawk.info,  Node: Very Simple,  Next: Two Rules,  Prev: Getting Started,  Up: Getting Started
  688.  
  689. A Very Simple Example
  690. =====================
  691.  
  692.    The following command runs a simple `awk' program that searches the
  693. input file `BBS-list' for the string of characters: `foo'.  (A string
  694. of characters is usually called, a "string".  The term "string" is
  695. perhaps based on similar usage in English, such as "a string of
  696. pearls," or, "a string of cars in a train.")
  697.  
  698.      awk '/foo/ { print $0 }' BBS-list
  699.  
  700. When lines containing `foo' are found, they are printed, because
  701. `print $0' means print the current line.  (Just `print' by itself means
  702. the same thing, so we could have written that instead.)
  703.  
  704.    You will notice that slashes, `/', surround the string `foo' in the
  705. actual `awk' program.  The slashes indicate that `foo' is a pattern to
  706. search for.  This type of pattern is called a "regular expression", and
  707. is covered in more detail later (*note Regular Expressions as Patterns:
  708. Regexp.).  There are single-quotes around the `awk' program so that the
  709. shell won't interpret any of it as special shell characters.
  710.  
  711.    Here is what this program prints:
  712.  
  713.      fooey        555-1234     2400/1200/300     B
  714.      foot         555-6699     1200/300          B
  715.      macfoo       555-6480     1200/300          A
  716.      sabafoo      555-2127     1200/300          C
  717.  
  718.    In an `awk' rule, either the pattern or the action can be omitted,
  719. but not both.  If the pattern is omitted, then the action is performed
  720. for *every* input line.  If the action is omitted, the default action
  721. is to print all lines that match the pattern.
  722.  
  723.    Thus, we could leave out the action (the `print' statement and the
  724. curly braces) in the above example, and the result would be the same:
  725. all lines matching the pattern `foo' would be printed.  By comparison,
  726. omitting the `print' statement but retaining the curly braces makes an
  727. empty action that does nothing; then no lines would be printed.
  728.  
  729. 
  730. File: gawk.info,  Node: Two Rules,  Next: More Complex,  Prev: Very Simple,  Up: Getting Started
  731.  
  732. An Example with Two Rules
  733. =========================
  734.  
  735.    The `awk' utility reads the input files one line at a time.  For
  736. each line, `awk' tries the patterns of each of the rules.  If several
  737. patterns match then several actions are run, in the order in which they
  738. appear in the `awk' program.  If no patterns match, then no actions are
  739. run.
  740.  
  741.    After processing all the rules (perhaps none) that match the line,
  742. `awk' reads the next line (however, *note The `next' Statement: Next
  743. Statement.).  This continues until the end of the file is reached.
  744.  
  745.    For example, the `awk' program:
  746.  
  747.      /12/  { print $0 }
  748.      /21/  { print $0 }
  749.  
  750. contains two rules.  The first rule has the string `12' as the pattern
  751. and `print $0' as the action.  The second rule has the string `21' as
  752. the pattern and also has `print $0' as the action.  Each rule's action
  753. is enclosed in its own pair of braces.
  754.  
  755.    This `awk' program prints every line that contains the string `12'
  756. *or* the string `21'.  If a line contains both strings, it is printed
  757. twice, once by each rule.
  758.  
  759.    If we run this program on our two sample data files, `BBS-list' and
  760. `inventory-shipped', as shown here:
  761.  
  762.      awk '/12/ { print $0 }
  763.           /21/ { print $0 }' BBS-list inventory-shipped
  764.  
  765. we get the following output:
  766.  
  767.      aardvark     555-5553     1200/300          B
  768.      alpo-net     555-3412     2400/1200/300     A
  769.      barfly       555-7685     1200/300          A
  770.      bites        555-1675     2400/1200/300     A
  771.      core         555-2912     1200/300          C
  772.      fooey        555-1234     2400/1200/300     B
  773.      foot         555-6699     1200/300          B
  774.      macfoo       555-6480     1200/300          A
  775.      sdace        555-3430     2400/1200/300     A
  776.      sabafoo      555-2127     1200/300          C
  777.      sabafoo      555-2127     1200/300          C
  778.      Jan  21  36  64 620
  779.      Apr  21  70  74 514
  780.  
  781. Note how the line in `BBS-list' beginning with `sabafoo' was printed
  782. twice, once for each rule.
  783.  
  784. 
  785. File: gawk.info,  Node: More Complex,  Next: Running gawk,  Prev: Two Rules,  Up: Getting Started
  786.  
  787. A More Complex Example
  788. ======================
  789.  
  790.    Here is an example to give you an idea of what typical `awk'
  791. programs do.  This example shows how `awk' can be used to summarize,
  792. select, and rearrange the output of another utility.  It uses features
  793. that haven't been covered yet, so don't worry if you don't understand
  794. all the details.
  795.  
  796.      ls -l | awk '$5 == "Nov" { sum += $4 }
  797.                   END { print sum }'
  798.  
  799.    This command prints the total number of bytes in all the files in the
  800. current directory that were last modified in November (of any year).
  801. (In the C shell you would need to type a semicolon and then a backslash
  802. at the end of the first line; in a POSIX-compliant shell, such as the
  803. Bourne shell or the Bourne-Again shell, you can type the example as
  804. shown.)
  805.  
  806.    The `ls -l' part of this example is a command that gives you a
  807. listing of the files in a directory, including file size and date.  Its
  808. output looks like this:
  809.  
  810.      -rw-r--r--  1 close        1933 Nov  7 13:05 Makefile
  811.      -rw-r--r--  1 close       10809 Nov  7 13:03 gawk.h
  812.      -rw-r--r--  1 close         983 Apr 13 12:14 gawk.tab.h
  813.      -rw-r--r--  1 close       31869 Jun 15 12:20 gawk.y
  814.      -rw-r--r--  1 close       22414 Nov  7 13:03 gawk1.c
  815.      -rw-r--r--  1 close       37455 Nov  7 13:03 gawk2.c
  816.      -rw-r--r--  1 close       27511 Dec  9 13:07 gawk3.c
  817.      -rw-r--r--  1 close        7989 Nov  7 13:03 gawk4.c
  818.  
  819. The first field contains read-write permissions, the second field
  820. contains the number of links to the file, and the third field
  821. identifies the owner of the file.  The fourth field contains the size
  822. of the file in bytes.  The fifth, sixth, and seventh fields contain the
  823. month, day, and time, respectively, that the file was last modified.
  824. Finally, the eighth field contains the name of the file.
  825.  
  826.    The `$5 == "Nov"' in our `awk' program is an expression that tests
  827. whether the fifth field of the output from `ls -l' matches the string
  828. `Nov'.  Each time a line has the string `Nov' in its fifth field, the
  829. action `{ sum += $4 }' is performed.  This adds the fourth field (the
  830. file size) to the variable `sum'.  As a result, when `awk' has finished
  831. reading all the input lines, `sum' is the sum of the sizes of files
  832. whose lines matched the pattern.  (This works because `awk' variables
  833. are automatically initialized to zero.)
  834.  
  835.    After the last line of output from `ls' has been processed, the
  836. `END' rule is executed, and the value of `sum' is printed.  In this
  837. example, the value of `sum' would be 80600.
  838.  
  839.    These more advanced `awk' techniques are covered in later sections
  840. (*note Overview of Actions: Actions.).  Before you can move on to more
  841. advanced `awk' programming, you have to know how `awk' interprets your
  842. input and displays your output.  By manipulating fields and using
  843. `print' statements, you can produce some very useful and spectacular
  844. looking reports.
  845.  
  846. 
  847. File: gawk.info,  Node: Running gawk,  Next: Comments,  Prev: More Complex,  Up: Getting Started
  848.  
  849. How to Run `awk' Programs
  850. =========================
  851.  
  852.    There are several ways to run an `awk' program.  If the program is
  853. short, it is easiest to include it in the command that runs `awk', like
  854. this:
  855.  
  856.      awk 'PROGRAM' INPUT-FILE1 INPUT-FILE2 ...
  857.  
  858. where PROGRAM consists of a series of patterns and actions, as
  859. described earlier.
  860.  
  861.    When the program is long, it is usually more convenient to put it in
  862. a file and run it with a command like this:
  863.  
  864.      awk -f PROGRAM-FILE INPUT-FILE1 INPUT-FILE2 ...
  865.  
  866. * Menu:
  867.  
  868. * One-shot::                    Running a short throw-away `awk' program.
  869. * Read Terminal::               Using no input files (input from
  870.                                 terminal instead).
  871. * Long::                        Putting permanent `awk' programs in files.
  872. * Executable Scripts::          Making self-contained `awk' programs.
  873.  
  874. 
  875. File: gawk.info,  Node: One-shot,  Next: Read Terminal,  Prev: Running gawk,  Up: Running gawk
  876.  
  877. One-shot Throw-away `awk' Programs
  878. ----------------------------------
  879.  
  880.    Once you are familiar with `awk', you will often type simple
  881. programs at the moment you want to use them.  Then you can write the
  882. program as the first argument of the `awk' command, like this:
  883.  
  884.      awk 'PROGRAM' INPUT-FILE1 INPUT-FILE2 ...
  885.  
  886. where PROGRAM consists of a series of PATTERNS and ACTIONS, as
  887. described earlier.
  888.  
  889.    This command format instructs the shell to start `awk' and use the
  890. PROGRAM to process records in the input file(s).  There are single
  891. quotes around PROGRAM so that the shell doesn't interpret any `awk'
  892. characters as special shell characters.  They also cause the shell to
  893. treat all of PROGRAM as a single argument for `awk' and allow PROGRAM
  894. to be more than one line long.
  895.  
  896.    This format is also useful for running short or medium-sized `awk'
  897. programs from shell scripts, because it avoids the need for a separate
  898. file for the `awk' program.  A self-contained shell script is more
  899. reliable since there are no other files to misplace.
  900.  
  901. 
  902. File: gawk.info,  Node: Read Terminal,  Next: Long,  Prev: One-shot,  Up: Running gawk
  903.  
  904. Running `awk' without Input Files
  905. ---------------------------------
  906.  
  907.    You can also run `awk' without any input files.  If you type the
  908. command line:
  909.  
  910.      awk 'PROGRAM'
  911.  
  912. then `awk' applies the PROGRAM to the "standard input", which usually
  913. means whatever you type on the terminal.  This continues until you
  914. indicate end-of-file by typing `Control-d'.
  915.  
  916.    For example, if you execute this command:
  917.  
  918.      awk '/th/'
  919.  
  920. whatever you type next is taken as data for that `awk' program.  If you
  921. go on to type the following data:
  922.  
  923.      Kathy
  924.      Ben
  925.      Tom
  926.      Beth
  927.      Seth
  928.      Karen
  929.      Thomas
  930.      `Control-d'
  931.  
  932. then `awk' prints this output:
  933.  
  934.      Kathy
  935.      Beth
  936.      Seth
  937.  
  938. as matching the pattern `th'.  Notice that it did not recognize
  939. `Thomas' as matching the pattern.  The `awk' language is "case
  940. sensitive", and matches patterns exactly.  (However, you can override
  941. this with the variable `IGNORECASE'.  *Note Case-sensitivity in
  942. Matching: Case-sensitivity.)
  943.  
  944. 
  945. File: gawk.info,  Node: Long,  Next: Executable Scripts,  Prev: Read Terminal,  Up: Running gawk
  946.  
  947. Running Long Programs
  948. ---------------------
  949.  
  950.    Sometimes your `awk' programs can be very long.  In this case it is
  951. more convenient to put the program into a separate file.  To tell `awk'
  952. to use that file for its program, you type:
  953.  
  954.      awk -f SOURCE-FILE INPUT-FILE1 INPUT-FILE2 ...
  955.  
  956.    The `-f' instructs the `awk' utility to get the `awk' program from
  957. the file SOURCE-FILE.  Any file name can be used for SOURCE-FILE.  For
  958. example, you could put the program:
  959.  
  960.      /th/
  961.  
  962. into the file `th-prog'.  Then this command:
  963.  
  964.      awk -f th-prog
  965.  
  966. does the same thing as this one:
  967.  
  968.      awk '/th/'
  969.  
  970. which was explained earlier (*note Running `awk' without Input Files:
  971. Read Terminal.).  Note that you don't usually need single quotes around
  972. the file name that you specify with `-f', because most file names don't
  973. contain any of the shell's special characters.  Notice that in
  974. `th-prog', the `awk' program did not have single quotes around it.  The
  975. quotes are only needed for programs that are provided on the `awk'
  976. command line.
  977.  
  978.    If you want to identify your `awk' program files clearly as such,
  979. you can add the extension `.awk' to the file name.  This doesn't affect
  980. the execution of the `awk' program, but it does make "housekeeping"
  981. easier.
  982.  
  983. 
  984. File: gawk.info,  Node: Executable Scripts,  Prev: Long,  Up: Running gawk
  985.  
  986. Executable `awk' Programs
  987. -------------------------
  988.  
  989.    Once you have learned `awk', you may want to write self-contained
  990. `awk' scripts, using the `#!' script mechanism.  You can do this on
  991. many Unix systems (1) (and someday on GNU).
  992.  
  993.    For example, you could create a text file named `hello', containing
  994. the following (where `BEGIN' is a feature we have not yet discussed):
  995.  
  996.      #! /bin/awk -f
  997.      
  998.      # a sample awk program
  999.      BEGIN    { print "hello, world" }
  1000.  
  1001. After making this file executable (with the `chmod' command), you can
  1002. simply type:
  1003.  
  1004.      hello
  1005.  
  1006. at the shell, and the system will arrange to run `awk' (2) as if you
  1007. had typed:
  1008.  
  1009.      awk -f hello
  1010.  
  1011. Self-contained `awk' scripts are useful when you want to write a
  1012. program which users can invoke without knowing that the program is
  1013. written in `awk'.
  1014.  
  1015.    If your system does not support the `#!' mechanism, you can get a
  1016. similar effect using a regular shell script.  It would look something
  1017. like this:
  1018.  
  1019.      : The colon makes sure this script is executed by the Bourne shell.
  1020.      awk 'PROGRAM' "$@"
  1021.  
  1022.    Using this technique, it is *vital* to enclose the PROGRAM in single
  1023. quotes to protect it from interpretation by the shell.  If you omit the
  1024. quotes, only a shell wizard can predict the results.
  1025.  
  1026.    The `"$@"' causes the shell to forward all the command line
  1027. arguments to the `awk' program, without interpretation.  The first
  1028. line, which starts with a colon, is used so that this shell script will
  1029. work even if invoked by a user who uses the C shell.
  1030.  
  1031.    ---------- Footnotes ----------
  1032.  
  1033.    (1)  The `#!' mechanism works on Unix systems derived from Berkeley
  1034. Unix, System V Release 4, and some System V Release 3 systems.
  1035.  
  1036.    (2)  The line beginning with `#!' lists the full pathname of an
  1037. interpreter to be run, and an optional initial command line argument to
  1038. pass to that interpreter.  The operating system then runs the
  1039. interpreter with the given argument and the full argument list of the
  1040. executed program.  The first argument in the list is the full pathname
  1041. of the `awk' program.  The rest of the argument list will either be
  1042. options to `awk', or data files, or both.
  1043.  
  1044. 
  1045. File: gawk.info,  Node: Comments,  Next: Statements/Lines,  Prev: Running gawk,  Up: Getting Started
  1046.  
  1047. Comments in `awk' Programs
  1048. ==========================
  1049.  
  1050.    A "comment" is some text that is included in a program for the sake
  1051. of human readers, and that is not really part of the program.  Comments
  1052. can explain what the program does, and how it works.  Nearly all
  1053. programming languages have provisions for comments, because programs are
  1054. typically hard to understand without their extra help.
  1055.  
  1056.    In the `awk' language, a comment starts with the sharp sign
  1057. character, `#', and continues to the end of the line.  The `awk'
  1058. language ignores the rest of a line following a sharp sign.  For
  1059. example, we could have put the following into `th-prog':
  1060.  
  1061.      # This program finds records containing the pattern `th'.  This is how
  1062.      # you continue comments on additional lines.
  1063.      /th/
  1064.  
  1065.    You can put comment lines into keyboard-composed throw-away `awk'
  1066. programs also, but this usually isn't very useful; the purpose of a
  1067. comment is to help you or another person understand the program at a
  1068. later time.
  1069.  
  1070.