home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / computer-lang / awk / faq
Encoding:
Internet Message Format  |  2002-06-25  |  54.7 KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!howland.erols.net!peer-feed.news.demon.net.MISMATCH!kibo.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
  2. From: awkfaq@locutus.ofB.ORG
  3. Newsgroups: comp.lang.awk,comp.answers,news.answers
  4. Subject: comp.lang.awk FAQ
  5. Followup-To: comp.lang.awk
  6. Date: 24 Jun 2002 11:00:00 -0600
  7. Organization: Private System, Edmonton, AB, Canada
  8. Approved: news-answers-request@MIT.EDU
  9. Expires: 25 Jul 2002 12:00:00 GMT
  10. Message-ID: <20020624.awkfaq.01@locutus.ofB.ORG>
  11. Reply-To: awkfaq@locutus.ofB.ORG
  12. Summary: FAQ (Frequently Asked Questions) about the awk programming language
  13. X-Trace: mail2news.demon.co.uk 1024941429 mail2news:28030 mail2news mail2news.demon.co.uk
  14. X-Complaints-To: abuse@demon.net
  15. X-Mail2News-Path: news.demon.net!scanner.worldgate.ca!alpha3.uucp!locutus.ofb.org
  16. User-Agent: trivdb (uufree)
  17. Lines: 1720
  18. Xref: senator-bedfellow.mit.edu comp.lang.awk:21243 comp.answers:50448 news.answers:232951
  19.  
  20. Archive-name: computer-lang/awk/faq
  21. Author: awkfaq@locutus.ofB.ORG (Russell Schulz)
  22. Comp-lang-awk-archive-name: faq
  23. Posting-Frequency: biweekly
  24. Last-modified: 2002-May-23
  25. Posting-Via: news.demon.net (mail2news)
  26. Not-Posting-Via: my connectivity provider who doesn't do news for uucp now
  27. Not-Posting-Via-The-Cable-Modem-Because: I don't want to
  28.  
  29. Frequently Asked Questions == FAQ
  30.  
  31. The FAQ list for comp.lang.awk can be found on the Internet:
  32.   <ftp://rtfm.mit.edu/pub/usenet/comp.lang.awk/faq>
  33.   <http://www.faqs.org/faqs/computer-lang/awk/faq/>
  34.  
  35. A version reformatted for PalmPilot may be found at
  36.   <http://www.castagnetto.org/download.php?fname=awkfaq.zip>
  37.  
  38. An Italian translation may be found at
  39.   <http://www.geocities.com/Eureka/Plaza/7153/i-awk-faq.html>
  40.   [error 1999-Jul-20]
  41.  
  42.  
  43. ========================================================================
  44.  
  45. Contents:
  46.  
  47.    1. Disclaimer
  48.    2. Spam
  49.    3. Can you answer my awk question?
  50.    4. How can I add a FAQ and its answer to the FAQ list?
  51.    5. What is awk?
  52.    6. What well-maintained awk-compatible languages are there?
  53.      6.1 nawk
  54.      6.2 gawk
  55.      6.3 mawk
  56.      6.4 tawk
  57.      6.5 mksawk
  58.      6.6 awkcc
  59.      6.7 awk2c
  60.      6.8 a2p
  61.      6.9 awka
  62.    7. Where can I buy awk?
  63.      7.1 AT&T (awk, awkcc)
  64.      7.2 Thompson Automation (tawk)
  65.      7.3 MKS (awk, can generate standalone interpreted .exe)
  66.    8. Where can I get awk for free?  For what platforms?
  67.      8.0 meta-answer
  68.      8.1 the one true awk
  69.      8.2 gawk
  70.        8.2.1 gawk precompiled for MS-DOS or OS/2
  71.        8.2.2 gawk precompiled for Macintosh
  72.        8.2.3 gawk precompiled for Risc OS on Acorn
  73.        8.2.4 jgawk (Japanese gawk)
  74.        8.2.5 gawk.dll
  75.      8.3 mawk
  76.      8.4 awk2c
  77.      8.5 awka
  78.      8.6 awk in scheme
  79.      8.98 various old binary-only distributions for MSDOS
  80.      8.99 awkcc
  81.    9. Why would anyone still use awk instead of perl?
  82.   10. How can I learn awk?
  83.   11. What are some other awk resources?
  84.   12. How do I report a bug in gawk?
  85.   13. What's wrong with gawk on Digital's OSF/1?
  86.   14. How can I access shell or environment variables in an awk script?
  87.     14.1 Environment variables in general
  88.     14.2 Unix Shell Quoting
  89.     14.3 ENVIRON and "env"|getline
  90.     14.4 exporting environment variables back to the parent process
  91.   15. Is there an easy way to determine if you have oawk or nawk?
  92.   16. How does awk deal with multiple files?
  93.     16.0 Version warning
  94.     16.1 How can awk test for the existence of a file?
  95.     16.2 How can I get awk to read multiple files?
  96.     16.3 How can I tell from which file my input is coming?
  97.     16.4 How can I get awk to open multiple files (selected at runtime)?
  98.     16.5 How can I treat the first file specially?
  99.     16.6 How can I explicitly pass in a filename to treat specially?
  100.   17. How many elements were created by split()?
  101.   18. How can I split a string into characters?
  102.   19. Why does SunOS/Solaris awk behave oddly?
  103.   20. How do I have dynamic-width printf strings, like C?
  104.   21. Why doesn't "\\$" behave like /\\$/ ?  Why don't parentheses match?
  105.   22. What is awk's exit code?
  106.   23. How can I get awk to be case-insensitive?
  107.   24. How can I force a numeric/non-numeric comparison?
  108.   25. Why does { FS=":"; print $1 } not split the first record?
  109.   26. Did ^ and $ and . change in gawk?
  110.   27. Why doesn't awk 'begin {...}' work?
  111.   28. Why does awk 'BEGIN { print 6 " " -22 }' lose the space?
  112.   29. How do I take advantage of gawk's networking support?
  113.   98. Miscellaneous
  114.   99. Credits
  115.  
  116.  
  117. ========================================================================
  118.  
  119. 1. Disclaimer
  120.  
  121. Read at your own risk.  The current, previous, or original authors
  122. make no claim as to fitness for any purpose or absence of any errors,
  123. and offer no warranty.  Do not eat.
  124.  
  125.  
  126. ========================================================================
  127.  
  128. 2. Spam
  129.  
  130. you wouldn't believe how much spam I get to this address.
  131.  
  132.  
  133. ========================================================================
  134.  
  135. 3. Can you answer my awk question?
  136.  
  137. Probably not.  Please don't mail it to me.
  138.  
  139. Read the FAQ, and the materials pointed to by it, and if you can't find
  140. an answer there, by all means post to the newsgroup.
  141.  
  142. If you need help posting, see <http://groups.google.com/> among others.
  143.  
  144. A FAQ list is intended to reduce traffic on a newsgroup, not eliminate it.
  145.  
  146.  
  147. ========================================================================
  148.  
  149. 4. How can I add a FAQ and its answer to the FAQ list?
  150.  
  151. Mail BOTH of them to me.  Then I can add them to the FAQ and it should
  152. help people who have that same question later, as well as everyone who
  153. reads the group, because they won't see it asked and answered so often.
  154.  
  155. I do not work on this FAQ every day, but I will try to get updates
  156. incorporated in a timely manner.
  157.  
  158. Of course, don't mail me my entire FAQ!  I already have a copy!  There
  159. are copies available all over the web that I could use if I lost mine!
  160. I pay for my access; don't you?
  161.  
  162.   
  163. ========================================================================
  164.  
  165. 5. What is awk?
  166.  
  167. awk is a programming language, named after its three original authors:
  168.  
  169.   Alfred V. Aho
  170.   Brian W. Kernighan
  171.   Peter J. Weinberger
  172.  
  173. they write:
  174.  
  175. ``
  176.   Awk is a convenient and expressive programming language that can be
  177.   applied to a wide variety of computing and data-manipulation tasks.
  178. ''
  179.  
  180. the title of the book uses `AWK', but the contents of the book
  181. use `awk' (except at the beginning of sentences, as above).  I
  182. will attempt to do the same (except perhaps at the beginning of
  183. sentences, as above).
  184.  
  185. most implementations of awk are interpreters which read your awk
  186. source program and parse it and act on it directly.
  187.  
  188. some vendors have developed awk compilers which will produce an
  189. `executable' that may be run stand-alone -- thus, the end user
  190. does not have access to the source code.  there are also various
  191. awk->C converters which allow you to achieve the same
  192. functionality (by compiling the resulting C code later).
  193.  
  194. one of the most popular compilers, from Thompson Automation,
  195. continues to be the subject of many positive posts in the group.
  196.  
  197.   [
  198.     I don't really want to start a reviews section, but it may be
  199.     appropriate.  I think it's of general interest, and a good thing
  200.     for the FAQ, but I don't want to be given any grief by a negative
  201.     review I didn't write just because I'm distributing it.
  202.  
  203.     if you have a review you'd like me to put a pointer to, please
  204.     inform me -- I already have some pointers of this form listed.
  205.   ]
  206.  
  207. comp.lang.awk is not particularly about sed; for sed discussion, see
  208. the sed FAQ (and other documents) for answers to common questions and
  209. group recommendations:
  210.  
  211.   <http://www.dbnet.ece.ntua.gr/~george/sed/sedfaq.html>
  212.   <http://www.wollery.demon.co.uk/sed_faq.txt>
  213.  
  214.   <http://www.student.northpark.edu/pemente/sed/index.htm>
  215.   <http://www.student.northpark.edu/pemente/sed/sedfaq.html>
  216.   <http://www.student.northpark.edu/pemente/sed/sed1line.txt>
  217.  
  218. this all seems unrelated to AWK Engineering AG at <http://www.awk.ch/>.
  219.  
  220.  
  221. ========================================================================
  222.  
  223. 6. What well-maintained awk-compatible languages are there?
  224.  
  225.   6.1 nawk
  226.     AT&T's `new awk' -- probably nobody uses the `old awk' anymore.
  227.     interpreter
  228.     might NOT be well-maintained
  229.  
  230.   6.2 gawk
  231.     from the GNU project
  232.     interpreter
  233.  
  234.   6.3 mawk
  235.     from Michael Brennan
  236.     interpreter
  237.  
  238.   6.4 tawk
  239.     from Thompson Automation
  240.     interpreter
  241.     compiler
  242.     MS-Windows DLL
  243.  
  244.   6.5 mksawk
  245.     interpreter
  246.     compiler
  247.     from Mortice Kern Systems (MKS)
  248.  
  249.     an old version of mksawk is shipped as `nawk' on Ultrix and
  250.     OSF/1.
  251.  
  252.   6.6 awkcc
  253.     translator to C
  254.     might NOT be well-maintained
  255.  
  256.   6.7 Brian Kernighan's awkc++
  257.     translator to C++
  258.     experimental
  259.     <http://cm.bell-labs.com/cm/cs/who/bwk/awkc++.ps>
  260.  
  261.   6.8 awk2c
  262.     translator to C
  263.     uses GNU awk libraries extensively, and is subject to GPL
  264.     might NOT be well-maintained
  265.  
  266.   6.9 a2p
  267.     translator to Perl
  268.     comes with Perl
  269.     didn't used to handle multiple concatenations:  e.g., var="x" "y" "z"
  270.       -> must be in pairs:  e.g.,  var=( "x" "y" ) "z"
  271.     didn't used to handle redirection:  e.g., { print("data") > "filename" }
  272.       -> no known workaround
  273.  
  274.   6.10 awka
  275.     translator to C (comes with library)
  276.     based on mawk
  277.     subject to GPL
  278.     <http://awka.sourceforge.net/>
  279.  
  280.  
  281. ========================================================================
  282.  
  283. 7. Where can I buy awk?
  284.  
  285. 7.1 AT&T (awk, awkcc)
  286.  
  287.   _The AWK Programming Language_ says:
  288.     phone
  289.       +1 201 522 6900 [is this number still valid?]
  290.     and login as `guest'.
  291.  
  292.   <http://www.unipress.com/att/new/awk.html>
  293.   <http://www.unipress.com/att/new/awkcc.html>
  294.  
  295.   these versions might NOT be well-maintained
  296.  
  297.   they might also have the old `99 fields' limitation
  298.  
  299. 7.2 Thompson Automation (tawk)
  300.  
  301.   <http://www.tasoft.com/tawk.html>
  302.  
  303.   Thompson Automation Software
  304.   5616 SW Jefferson
  305.   Portland, OR   97221
  306.   USA
  307.  
  308.   North America: 800-944-0139
  309.   Phone: +1 503 224 1639
  310.   Fax: +1 503 224 3230
  311.  
  312. 7.3 MKS (awk, can generate standalone interpreted .exe)
  313.  
  314.   <http://www.mks.ca/solution/tk/>
  315.  
  316.   Mortice Kern Systems
  317.   185 Columbia Street W
  318.   Waterloo, ON
  319.   N2L 5Z5
  320.   Canada
  321.  
  322.   North America: 800-265-2797
  323.   Phone: +1 519 884 2251
  324.   Fax: +1 519 884 8861
  325.  
  326.  
  327. ========================================================================
  328.  
  329. 8. Where can I get awk for free?  For what platforms?
  330.  
  331.   8.0 meta-answer
  332.     Obtaining Awk and Perl
  333.     <http://www.crossmyt.com/hc/htmlchek/awk-perl.html>
  334.  
  335.   8.1 the one true awk
  336.     <http://cm.bell-labs.com/who/bwk/>
  337.     <http://plan9.bell-labs.com/who/bwk/>
  338.     <ftp://netlib.bell-labs.com/netlib/research/awk.bundle.Z>
  339.       [ appears to no longer be available via ftp 1997/Oct/23 ]
  340.  
  341.     This is the version of awk described in "The Awk Programming Language",
  342.     by A. V. Aho, B. W. Kernighan, and P. J. Weinberger
  343.     (Addison-Wesley, 1988, ISBN 0-201-07981-X).
  344.     Changes, mostly bug fixes, are listed in FIXES.
  345.  
  346.       8.1.1 the one true awk precompiled for Win32 is available from the
  347.       locations in 8.1; MS-DOS and OS/2 executables are available in the
  348.       GNUish collection:
  349.  
  350.         <http://www.simtel.net/simtel.net/>
  351.         <ftp://ftp.simtel.net/pub/simtelnet/gnu/gnuish>
  352.  
  353.   8.2 gawk
  354.     NOTE:  gawk 3.0.2 had a per-record memory leak which was fixed
  355.       for gawk 3.0.3 .
  356.  
  357.     <ftp://ftp.gnu.org/gnu/gawk/>
  358.     e.g.,
  359.       <ftp://ftp.gnu.org/gnu/gawk/gawk-3.1.0.tar.gz>
  360.  
  361.       8.2.1 gawk precompiled for MS-DOS, Win32, or OS/2
  362.  
  363.         The djgpp collection contains a 32-bit DOS gawk, along with
  364.         many GNU utilities which may be useful with gawk (djgpp ports
  365.         understand long filenames on Windows 95):
  366.  
  367.         <http://www.simtel.net/simtel.net/>
  368.         <ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/>
  369.           (look for gwk*.zip)
  370.  
  371.         32-bit DOS (djgpp), Win32, and 16-bit OS/2 and DOS versions are
  372.         part of the GNUish project:
  373.  
  374.         <http://www.simtel.net/simtel.net/>
  375.         <ftp://ftp.simtel.net/pub/simtelnet/gnu/gnuish/>
  376.         <http://wuarchive.wustl.edu/systems/msdos/gnuish/> [error 1998/Apr/16]
  377.         <http://simtel.coast.net/SimTel/gnu/gnuish.html> [defunct]
  378.  
  379.         32-bit OS/2, Win32, and DOS (emx) versions:
  380.  
  381.         <http://www.leo.org/pub/comp/os/os2/leo/gnu/script/gnuawk.zip> (DE)
  382.         <ftp://ftp-os2.cdrom.com/pub/os2/lang/gnuawk.zip>              (US)
  383.  
  384.         32-bit Win32 version (cygwin)
  385.  
  386.         <http://www.cygwin.com/>
  387.  
  388.         (CygWin requires some `buy in' to the CygWin Way -- this may not
  389.         affect you, or it may make your life much easier, or it may break
  390.         some small things you currently do.  In particular, I have been
  391.         bitten by CRLF-vs-LF issues and no-drive-letters issues.)
  392.  
  393.       8.2.2 gawk precompiled for Macintosh
  394.  
  395.         <ftp://ftp.funet.fi/pub/mac/programming/gawk.sit>
  396.         <ftp://ezinfo.ethz.ch/mac/programming/gnu-awk-211.> [note trailing .]
  397.         <ftp://ftp.uwtc.washington.edu/pub/Mac/Programming/>
  398.         <ftp://ftp.eos.hokudai.ac.jp/pub/mac/util/Gawk/>
  399.         <ftp://ftp.cs.tu-berlin.de/pub/mac/lang/MPW/>
  400.  
  401.       8.2.3.1 gawk 3.0.3 for Risc OS (versions >= 3.1) on Acorn
  402.         Binary ported by J.Kortink, available from
  403.         <http://www.inter.nl.net/users/J.Kortink/>
  404.         Wimp front end available from
  405.         <http://www.wraith.u-net.com/soft.html>
  406.  
  407.       8.2.3.2 gawk 3.0.4 for Risc OS on Acorn
  408.         Binary (with extensions) ported by Gavin Wraith, available from
  409.         <http://www.wraith.u-net.com/arc/Gawk304.arc >
  410.  
  411.       8.2.4 jgawk (Japanese gawk)
  412.         <ftp://ftp.eos.hokudai.ac.jp/pub/mac/util/jgawk/>
  413.         <ftp://ftp.fu-berlin.de/mac/mirrors/info-mac/text/jgawk-215.hqx>
  414.  
  415.       8.2.5 gawk.dll
  416.         <http://www.walkerj.muc.de/>
  417.           old Gawk 2.15.2 (from 1995) plus extensions
  418.           + read/Write functions for INI files
  419.           + read-only functions for DBF files
  420.           this is a _16-bit_ DLL, unfortunately without thunks
  421.           works with Win3.1x, plus Win9x _from 16-bit callers_
  422.           intermittent pointer problems with complex regexs
  423.  
  424.   8.3 mawk
  425.     NOTE:  do not use mawk 1.3.2 (a one-character change yields 1.3.3)
  426.       due to an obscure (and rarely-appearing) regex problem.
  427.  
  428.     <ftp://ftp.whidbey.net/pub/brennan/>
  429.       e.g.,
  430.       <ftp://ftp.whidbey.net/pub/brennan/mawk1.3.3.tar.gz>
  431.  
  432.       8.3.1 mawk 1.3.3 for Risc OS on Acorn
  433.         Binary (with extensions) ported by Gavin Wraith, available from
  434.         <http://www.wraith.u-net.com/arc/Mawk133.arc>
  435.  
  436.   8.4 awk2c
  437.     <ftp://sunsite.unc.edu/pub/Linux/utils/text/awk2c050.tgz>
  438.  
  439.   8.5 awka
  440.     <http://awka.sourceforge.net/>
  441.  
  442.   8.6 awk in scheme
  443.     <http://charles.lehalle.free.fr/tools.html>
  444.  
  445.   8.98 various old binary-only distributions for MSDOS
  446.     <ftp://ftp.sunsite.org.uk/Mirrors/simtel.coast.net/coast/msdos/awk/>
  447.     <http://www.simtel.net/pub/msdos/txtutl/>
  448.  
  449.   8.99 awkcc [unknown]
  450.     previous versions of this FAQ mentioned a file on MKS' web site.
  451.     Neil Mahoney spent some time examining it and discovered it is
  452.     just a package BUILT with awkcc, not awkcc itself.  eventually,
  453.     this notice will be removed.
  454.  
  455. some of Neil's explorations are interesting for those looking
  456. for the real awkcc:
  457.  
  458. Get file copied to my Sun 4 file system (where I do most of my work)
  459.  
  460. $ ls -l awkcc
  461. -rw-r--r--  1 neilm        1827 May 20 11:20 README
  462. -rw-r--r--  1 neilm        1378 May 20 11:20 awk.h
  463. -rwxr-xr-x  1 neilm         118 May 20 11:20 awkcc.sh
  464. -rw-r--r--  1 neilm         824 May 20 11:20 dollars.h
  465. -rw-r--r--  1 neilm        3858 May 20 11:20 ear.h
  466. -rw-r--r--  1 neilm         993 May 20 11:20 hash.h
  467. -rw-r--r--  1 neilm        1707 May 20 11:20 header.h
  468. -rw-r--r--  1 neilm      103468 May 20 11:21 libAWK.a
  469. -rw-r--r--  1 neilm        4136 May 20 11:20 specassign.h
  470. -rw-r--r--  1 neilm       14467 May 20 11:20 unipen.c
  471. -rw-r--r--  1 neilm         275 May 20 11:20 y.tab.h
  472.  
  473. Looking good!
  474.  
  475. $ awkcc.sh
  476. fix errors found by run
  477. $ awkcc.sh
  478.  
  479. no awkcc executable... what is this ?
  480. -rwxr-xr-x  1 neilm      106496 May 20 11:21 uniparse
  481.  
  482. $ head -20 README
  483.  
  484.         ##############################################
  485.         To compile the UNIPEN 1.0 parser, run awkcc.sh
  486.         ##############################################
  487.  
  488. The following files are from the awkcc package,
  489. provided by Chris Ramming (jcr@research.att.com),
  490. Copyright (c) 1991 AT&T.
  491. All Rights Reserved
  492.  
  493. awk.h                              awkcc.sh
  494. copyright                          dollars.h
  495. ear.h                              hash.h
  496. header.h                           libAWK.a
  497. specassign.h                       y.tab.h
  498.  
  499. The file unipen.c is machine generated c code from the unipen.awk
  500. parser, using the awkcc package.
  501. Copyright (c) 1994 - I. Guyon, AT&T Bell Labs.
  502.  
  503. #  DISCLAIMER:                        #
  504.  
  505.  
  506. ========================================================================
  507.  
  508. 9. Why would anyone still use awk instead of perl?
  509.  
  510.   a valid question, since awk is a subset of perl (functionally, not
  511.   necessarily syntactically); also, the authors of perl have usually
  512.   known awk (and sed, and C, and a host of other Unix tools) very well,
  513.   and still decided to move on.
  514.  
  515.   there are some things that perl has built-in support for that almost
  516.   no version of awk can do without great difficulty (if at all); if you
  517.   need to do these things, there may be no choice to make.  for instance,
  518.   no reasonable person would try to write a web server in awk instead
  519.   of using perl or even C, if the actual socket programming has to be
  520.   written in traditional awk.  however, gawk 3.1.0's /inet and ftwalk's
  521.   built-in networking primitives may remove this particular limitation.
  522.  
  523.   however, there are some things in awk's favor compared to perl:
  524.  
  525.   - awk is simpler (especially important if deciding which to learn first)
  526.   - awk syntax is far more regular (another advantage for the beginner,
  527.     even without considering syntax-highlighting editors)
  528.   - you may already know awk well enough for the task at hand
  529.   - you may have only awk installed
  530.   - awk can be smaller, thus much quicker to execute for small programs
  531.   - awk variables don't have `$' in front of them :-)
  532.   - clear perl code is better than unclear awk code; but NOTHING comes
  533.     close to unclear perl code
  534.  
  535.   Tom Christiansen wrote in Message-ID: <3766d75e@cs.colorado.edu>
  536.   > Awk is a venerable, powerful, elegant, and simple tool that everyone
  537.   > should know.  Perl is a superset and child of awk, but has much more
  538.   > power that comes at expense of sacrificing some of that simplicity.
  539.  
  540.  
  541. ========================================================================
  542.  
  543. 10. How can I learn awk?
  544.  
  545.   The commercial vendors of DOS versions (MKS and Thompson) each have
  546.   their own well written books with examples.  [available separately?]
  547.  
  548.   English Book:
  549.  
  550.       _The AWK Programming Language_, by Aho, Kernighan and Weinberger,
  551.       who invented the language.  Published by Addison-Wesley.  Lots of
  552.       good material in not a lot of space.  A little out of date
  553.       with regard to POSIX awk.
  554.  
  555.       ISBN 0-201-07981-X
  556.  
  557.       <http://cm.bell-labs.com/cm/cs/awkbook/>
  558.       <http://cseng.aw.com/book/0,3828,020107981X,00.html>
  559.         [ text looks mangled at the beginning ]
  560.  
  561.   English Book:
  562.  
  563.       Effective Awk Programming
  564.       ISBN 0-596-00070-7 (third edition)
  565.  
  566.       <http://www.oreilly.com/catalog/awkprog3/>
  567.         (includes link to DocBook source)
  568.       <http://www.gnu.org/manual/gawk>
  569.  
  570.       Russell recommends buying the book instead of trying to print it
  571.       all out, for three reasons:
  572.  
  573.         1. it's probably cheaper than using your own toner and paper.
  574.  
  575.         2. some money goes back to help further development, both to
  576.            Arnold Robbins (only if you buy from ORA) and the Free
  577.            Software Foundation (if you buy from either ORA or the FSF).
  578.  
  579.         3. it helps convince publishers that we _like_ having full
  580.            documentation available on-line (e.g., for searching), but
  581.            will still pay for a compact, bound copy.
  582.  
  583.       information, including an errata list, is on the web site.
  584.  
  585.  
  586.       older editions, historical interest only:
  587.  
  588.       _Effective AWK Programming_ by Arnold Robbins.  Published by
  589.       SSC (+1 206-FOR-UNIX, <http://www.ssc.com/>,
  590.       <mailto:sales@ssc.com>).  Also published by the FSF as
  591.       "The GNU AWK User's Guide"; Texinfo source is included with
  592.       the gawk distribution, so you can also print this yourself.
  593.  
  594.       A highly-praised reference card is also included.
  595.  
  596.       ISBN 1-57831-000-8 (second edition)
  597.  
  598.       <http://www.ssc.com/ssc/eap/>
  599.  
  600.       ISBN 0-916151-88-3 (first edition)
  601.  
  602.   English reference card:
  603.  
  604.       ISBN 1-56592-729-X
  605.       <http://www.oreilly.com/catalog/sedawkrepr/>
  606.  
  607.   English Book:
  608.  
  609.       second edition:
  610.  
  611.       _Sed & Awk_, by Dale Dougherty & Arnold Robbins, published
  612.       by O'Reilly and Associates.
  613.  
  614.       _sed & awk_ describes two text manipulation programs that are
  615.       mainstays of the UNIX programmer's toolbox.  This new edition
  616.       covers the sed and awk programs as they are now mandated by
  617.       the POSIX standard and includes discussion of the GNU versions
  618.       of these programs.
  619.  
  620.       <http://www.ora.com/catalog/sed2/>
  621.       <http://www.ora.com/catalog/covers/sedawk-t.gif>
  622.  
  623.       ISBN 1-56592-225-5
  624.  
  625.       An errata for the second edition of Sed & Awk is at
  626.  
  627.       <http://www.cs.colostate.edu/~dzubera/sedawk2.txt>
  628.  
  629.       historical notes on the first edition:
  630.  
  631.       _Sed & Awk_, by Dale Dougherty, published by O'Reilly and
  632.       Associates.  A nice introduction to sed and awk, showing how
  633.       they relate to each other.  However, the first edition is
  634.       `full of typos and out-and-out mistakes'.
  635.  
  636.       <http://www.ora.com/catalog/sed/>
  637.  
  638.       ISBN 0-937175-59-5
  639.  
  640.       a `by no means complete' errata list is available.
  641.       the author mentions `later printings of the book have
  642.       many of the errors fixed.'
  643.  
  644.       <http://www.cs.colostate.edu/~dzubera/sedawk.txt>
  645.  
  646.   English Book:
  647.  
  648.       _Unix awk and sed programmer's interactive workbook_
  649.  
  650.       ISBN 0-13-082675-8
  651.  
  652.       <http://www.phptr.com/phptrinteractive/>
  653.       <http://www.phptr.com/phptrinteractive/unix/>
  654.       <http://cw.prenhall.com/patsis/>
  655.  
  656.       How can people publish Unix books that mix up ` and ' ?!?!
  657.       (Example:  "sed `s/://'" won't do what they seem to think it
  658.       will.)  And why create a frames-only Java-only
  659.       Navigator/IE-centric gratuitously-incompatible website?!
  660.  
  661.       None of these things entice me to actually read the book
  662.       in any depth.
  663.  
  664.   Deutsch Book:
  665.  
  666.       awk und sed 
  667.       Helmut Herold
  668.       ISBN 3-89319-685-4
  669.  
  670.       <http://www.addison-wesley.de/projector/projector.asp?page=bookdetails&isbn=3893196854>
  671.       <http://www.addison-wesley.de/katalog/item.ppml?textexpr=awk&id=00018>
  672.  
  673.   Deutsch Book:
  674.  
  675.       Linux-Unix Profitools, by Helmut Herold.
  676.       awk, sed, lex, yacc, und make 
  677.  
  678.       <http://www.addison-wesley.de/projector/projector.asp?page=bookdetails&isbn=3827314488>
  679.       <http://www.addison-wesley.de/katalog/item.ppml?textexpr=awk&id=00287>
  680.  
  681.       ISBN 3-8273-1448-8
  682.  
  683.   English Book:
  684.  
  685.       _Mastering Regular Expressions_, by Jeffrey E.F. Friedl, published
  686.       by O'Reilly and Associates.  (the `Hip Owls Book')
  687.  
  688.       ``... you will learn how to use regular expressions to
  689.       solve problems and get the most out of tools that provide
  690.       them.  Not only that, but much more:  this book is about
  691.       _mastering_ regular expressions.''
  692.  
  693.       <http://www.ora.com/catalog/regex/>
  694.  
  695.       errata, additions, change log available at the author's home page
  696.       <http://public.yahoo.com/~jfriedl/regex/>
  697.  
  698.       ISBN 1-56592-257-3
  699.  
  700.   Deutsch Book:
  701.  
  702.       Friedl's _Mastering Regular Expressions_.
  703.  
  704.       <http://www.oreilly.de/catalog/regexger/index.html>
  705.  
  706.   Web Site:
  707.  
  708.       <http://www.cs.hmc.edu/qref/awk.html>
  709.  
  710.       Getting started with Awk
  711.  
  712.   Web Site:
  713.  
  714.       <http://www.uga.edu/~ucns/wsg/unix/awk/>
  715.  
  716.       Awk introduction
  717.  
  718.   Web Site:
  719.  
  720.       <http://www.mbnet.mb.ca/~natewild/awk/awk.html>
  721.         [ no longer available 1997/Oct/28 ]
  722.  
  723.       Information about Tawk; Awk sample source code
  724.  
  725.   Ian Gordon's Introduction to Gawk from Linux Journal
  726.  
  727.       <http://www.ssc.com/lj/issue25/1156.html>
  728.  
  729.   Juergen Kahrs' Gawk 3.1 introduction from Linux Journal
  730.  
  731.       <http://www.ssc.com/lj/issue60/>
  732.  
  733.   Awk Introduction
  734.  
  735.       <http://www.softlab.ece.ntua.gr/facilities/documentation/unix/docs/awk.ps>
  736.       <http://www.softlab.ece.ntua.gr/facilities/documentation/unix/docs/awk.txt>
  737.       <ftp://www.brooks.af.mil/pub/unix/white_papers/awk.ps>
  738.       [error 2001/Apr/07]
  739.  
  740.       Awk introduction (PostScript and text) by awk authors
  741.       (somewhat old, doesn't cover the many recent extensions,
  742.       but still a valid introduction to the language)
  743.  
  744.   Web Site:
  745.  
  746.       <http://www.shelldorado.com/articles/awkcompat.html>
  747.  
  748.       Awk compatibility
  749.  
  750.   Web Site:
  751.  
  752.       <http://www.canberra.edu.au/~sam/whp/awk-guide.html>
  753.  
  754.       How to get things done in Awk
  755.  
  756.   Web Site:
  757.  
  758.       <http://www.novia.net/~phridge/programming/awk/>
  759.  
  760.       Awk Programming examples
  761.  
  762.   Japanese Book:
  763.  
  764.       _Tanoshii UNIX -UNIX he no shoutai-_, by Aya Sakamoto
  765.       (UNIX is fun -an invitation to UNIX-)
  766.       ASCII October 1990
  767.       <http://www.ascii.co.jp/>
  768.       ISBN 4-7561-0785-0
  769.       A step by step UNIX tutorial for novices. Long chapter about
  770.       awk, which follows chapters about grep and sed, gives good
  771.       advice on general expressions and how awk is used in practice.
  772.  
  773.   Japanese Book:
  774.  
  775.      _Grep,Sed,Awk_ by Akihiro Miyoshi
  776.      ISBN  4-87966-794-3
  777.      June 1998 264 pages
  778.      Shuwa System Manual & Reference Series
  779.      <http://www.shuwasystem.co.jp/books/wwwsrch/cgi-bin/content/794/index.htm>
  780.      Serves both as a tutorial and a manual. Divided quite evenly into three
  781.      parts. Regular expressions explored in detail in grep section.
  782.  
  783.   Japanese Book:
  784.  
  785.     _Awk wo 256 bai tsukau tame no hon_ by Shimura, Washikita and Nishimura 
  786.     ASCII  August 1993 277 pages
  787.     ISBN 4-75610-162-3
  788.  
  789.     Manic book titled 'The Book to Use Awk 256 Times More'. Good
  790.     illustrations and interesting insights. Lists '100 liners': a game,
  791.     fortune telling etc. 
  792.  
  793.   English Booklet:
  794.  
  795.     TCP/IP Internetworking With Gawk
  796.     ISBN 1-882114-93-0
  797.     <http://home.vr-web.de/Juergen.Kahrs/gawk/gawkinet.html>
  798.  
  799.     An abridged form is included in O'Reilly's Effective Awk Programming 3e
  800.  
  801.     Hard copy also available as book on demand from
  802.     <http://www.lob.de>
  803.  
  804.  
  805. ========================================================================
  806.  
  807. 11. What are some other awk resources?
  808.  
  809.   Alta Vista awk Related Searches (inconspicuously placed under the
  810.   search edit box, given to graphical browsers only)
  811.  
  812.     <http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&q=awk>
  813.  
  814.   Awk collections in various search engines
  815.  
  816.     <http://dir.yahoo.com/Computers_and_Internet/Programming_Languages/awk/>
  817.     <http://dir.altavista.com/Computers/Programming/Languages/Awk.shtml>
  818.     <http://www.dmoz.org/Computers/Programming/Languages/Awk/>
  819.  
  820.   Arnold Robbins' collection
  821.  
  822.       <ftp://ftp.freefriends.org/arnold/Awkstuff/>
  823.  
  824.       > A collection of awk related stuff that I'm starting and willing
  825.       > to maintain.  Currently includes Henry Spencer's awf and aaa
  826.       > programs, Brian Kernighan's chem, and Nelson Beebe's awkpretty,
  827.       > and some smaller items.
  828.  
  829.   Awk quick reference (in plain ASCII and PalmPilot format)
  830.  
  831.       <http://www.castagnetto.org/download.php?fname=awkqref.zip>
  832.  
  833.   Unix and awk courseware
  834.  
  835.       <http://www.cit.ac.nz/smac/unix/default.htm>
  836.  
  837.   Awk course
  838.  
  839.       <http://www.ee.ic.ac.uk/course/advanced/awk/awk.html>
  840.       [error 2001/Apr/07]
  841.  
  842.   Developer information on awk
  843.  
  844.       <http://www.devinfo.com/languages/awk/>
  845.  
  846.   Spatial Analysis with Awk (course)
  847.  
  848.       <http://www.udel.edu/johnmack/frec682/682awk.html>
  849.  
  850.   Debugger and Assertion Checker for Awk
  851.  
  852.       <http://www.irisa.fr/EXTERNE/manifestations/AADEBUG95/Abstracts/auguston2.html>
  853.       [error 2001/Apr/07]
  854.  
  855.   Free Compilers and Interpreters List
  856.  
  857.       <http://www.idiom.com/free-compilers/LANG/awk-1.html>
  858.  
  859.   Voicenet.com awk page
  860.  
  861.       <http://www.voicenet.com/tech/comp/prog/awk/>
  862.       [error 2001/Apr/07]
  863.  
  864.   Four awk implementations for MS-DOS:  How do they compare?
  865.  
  866.       <http://www.voicenet.com/tech/comp/prog/awk/awk2.rev>
  867.       [error 2001/Apr/07]
  868.  
  869.   Gawk 3 manual
  870.  
  871.       <http://sunsite.ualberta.ca/Documentation/Info/by-chapter/gawk-3.0.3/>
  872.       <ftp://sunsite.ualberta.ca/pub/Mirror/gnu/gawk/>
  873.  
  874.   Unix Vault
  875.  
  876.       <http://www2.shore.net/~jblaine/vault/>
  877.       [error 2001/Apr/07]
  878.  
  879.   Yahoo's awk links
  880.  
  881.       <http://www.yahoo.com/Computers_and_Internet/Programming_Languages/Awk/>
  882.  
  883.   New Mexico Tech awk information
  884.  
  885.       <http://www.nmt.edu/tcc/help/lang/awk.html>
  886.       <http://www.nmt.edu/bin/man?awk> [empty 1998/Apr/16]
  887.       <http://www.nmt.edu/bin/man?gawk>
  888.  
  889.   A Supplemental Document For AWK
  890.   - or -
  891.   Things Al, Pete, And Brian Didn't Mention Much
  892.  
  893.       <http://oak.oakland.edu/pub/unix-c/info/awk-supplement.txt>
  894.       [error 2001/Apr/07]
  895.  
  896.       [
  897.         interesting historically -- I always wondered exactly why I
  898.         mistrusted setting `$n' and expecting `$0' to change -- and this
  899.         document explains why.  (it has since become standard behavior.)
  900.       ]
  901.  
  902.   A* - an awk extension (paper by D.A.Ladd and J.C.Ramming)
  903.  
  904.       <http://sunsite.ust.hk/dblp/db/journals/tse/tse21.html>
  905.       [error 2001/Apr/07]
  906.       <http://www.usenix.org/~jcr/> [error 1998/Apr/16]
  907.  
  908.       [ does anyone know what issue of _Unix Review_ mentioned it? ]
  909.  
  910.   Konrad Hambrick's `rawketry' and AltAcc data reduction scripts
  911.  
  912.       <ftp://ftp.netcom.com/pub/ko/konrad/rawketry/>
  913.       <ftp://ftp.netcom.com/pub/ko/konrad/altacc/software/>
  914.  
  915.   Ralph Becket's CGI and HTML Awk libraries
  916.  
  917.       <http://www.cam.sri.com/people/becket/awk/awk.html>
  918.       [error 2001/Apr/07]
  919.  
  920.   E. Stiltner's creation of HTML tables with awk
  921.  
  922.       <http://www.sni.net/sccs/sccsadoc.htm>
  923.       [error 2001/Apr/07]
  924.  
  925.   ftwalk / hawk
  926.  
  927.   > a language that attempts to scale awk principles up to
  928.   > a level competitive with Perl, Python, etc. Run as ftwalk,
  929.   > it does a file tree walk (think of find+awk). Run as hawk,
  930.   > it runs awk scripts (not quite compatibly).
  931.  
  932.       <http://www.tomhull.com/ocston/projects/hawk.html>
  933.       <http://ftwalk.sourceforge.net/>
  934.  
  935.   Data Junction Content Extraction Language (DJ CXL)
  936.  
  937.       similar to awk
  938.       <http://www.datajunction.com/products/djxl.html>
  939.  
  940.   English Book
  941.  
  942.       Language and Computers
  943.       ISBN 0-7486-0785-4 (paperback)
  944.       ISBN 0-7486-0848-6 (hardcover)
  945.       New Scientist #2071, 1997/Mar/01, p45; paragraph titled `Dream in awk'
  946.       <http://www.ling.lancs.ac.uk/staff/andrew/etel.htm>
  947.       <http://www.ling.lancs.ac.uk/staff/andrew/pubs.htm>
  948.       <http://www.eup.ed.ac.uk/>
  949.       <http://www.eup.ed.ac.uk/cgi/odbic.exe?input=NewWeb/Books/Barnbrook9856.htm>
  950.  
  951.       [ Blackwell's in Oxford has 3 copies under General Linguistics ]
  952.  
  953.       a book about computer-aided linguistics which uses awk as its
  954.       implementation language
  955.  
  956.   English Lecture Notes; Combining sh, sed, and awk for language analysis
  957.  
  958.       <ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz>
  959.       <ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz>
  960.  
  961.   Consultix:  Awk Lecture / Lab courses (instructor-led)
  962.  
  963.       <http://www.consultix-inc.com/>
  964.  
  965.   How to get started with AWK
  966.  
  967.       <http://www.bolthole.com/AWK.html>
  968.  
  969.   awk resources for the Acorn RISC OS
  970.  
  971.       <http://www.awk.riscos.org.uk/>
  972.  
  973.   Troy A. Tiritilli's Awk Overview
  974.  
  975.       <http://www.students.stedwards.edu/~ttiriti/pl.htm
  976.       (email me; stedwards.edu mail setup is broken)
  977.  
  978.   Henry Spencer's awf (Amazingly Workable Formatter)
  979.  
  980.       <http://www.simtel.net/simtel.net/>
  981.       <ftp://ftp.zoo.toronto.edu/>
  982.       (more precise URL eventually...)
  983.  
  984.   Jon-Egil Korsvold's awk archive
  985.  
  986.       <http://www.kortext.no/awk.htm> (English)
  987.       <http://www.kortext.no/awkno.htm> (Norwegian)
  988.  
  989.  
  990. ========================================================================
  991.  
  992. 12. How do I report a bug in gawk?
  993.  
  994. This is described in great detail in the gawk documentation.  In brief:
  995.  
  996.    1. Make sure what you've discovered is really a bug by checking
  997.       the documentation and, if possible, comparing with nawk and mawk.
  998.  
  999.    2. Cut down the program and data to as small as possible a test
  1000.       case that will illustrate the bug.
  1001.  
  1002.    3. Optionally post to comp.lang.awk; this allows others to confirm
  1003.       or deny the behavior, and its incorrectness (or lack thereof).
  1004.  
  1005.    4. Send mail to <mailto:bug-gawk@gnu.org>.  This automatically sends
  1006.       a copy to Arnold Robbins.  Do not JUST post in comp.lang.awk;
  1007.       Arnold's readership there is sporadic, and of course any Usenet
  1008.       article can be missed, killed, or dropped.
  1009.  
  1010.  
  1011. ========================================================================
  1012.  
  1013. 13. What's wrong with gawk on Digital's OSF/1?
  1014.  
  1015. The version of gawk shipped with OSF/1 is very old, based on gawk
  1016. 2.14.  Get the current version from a GNU mirror near you, and if
  1017. you still have a problem, report it as per the directions in the
  1018. gawk documentation.
  1019.  
  1020.  
  1021. ========================================================================
  1022.  
  1023. 14. How can I access shell or environment variables in an awk script?
  1024.  
  1025. 14.0 shells
  1026.  
  1027. the examples using quoting are intended for use with any standard
  1028. (sh-compatible-quoting) Unix shell.  as with all complex quoting,
  1029. all these examples become much easier to work with (or under DOS
  1030. and MS-Windows, less impossible) when put in a file and invoked with
  1031. `awk -f filename.awk' instead.
  1032.  
  1033. non-sh-compatible shells will require different quoting.  if you're
  1034. not even using Unix (or a ported Unix shell), just ignore the whole
  1035. section on quoting.
  1036.  
  1037. 14.1 Environment variables in general
  1038.  
  1039. Answer 1:  on Unix, use "alternate quoting", e.g.
  1040.  
  1041.         awk -F: '$1 ~ /'"$USER"'/ {print $5}' /etc/passwd
  1042.                 ^^^^^^^^*******^^^^^^^^^^^^^^
  1043.  
  1044.         any standard Unix shell will send the underlined part as one
  1045.         long argument (with embedded spaces) to awk, for instance:
  1046.  
  1047.         $1 ~ /bwk/ {print $5}
  1048.  
  1049.         Note that there may not be any spaces between the quoted
  1050.         parts.  Otherwise, you wouldn't end up a single, long script
  1051.         argument, because Unix shells break arguments on spaces
  1052.         (unless they are `escaped' with `\', or in '' or "", as the
  1053.         above example shows).
  1054.  
  1055. Answer 2:  RTFM to see if and how your awk supports variable definitions
  1056.            on the command line, e.g.,
  1057.  
  1058.         awk -F: -v name="$USER" '$1 ~ name {print $5}' /etc/passwd
  1059.  
  1060. Answer 3:  RTFM if your awk can access enviroment vars.  Then perhaps
  1061.  
  1062.         awk -F: '$1 ~ ENVIRON["USER"] {print $5}' /etc/passwd
  1063.  
  1064.         Always remember for your /bin/sh scripts that it's easy to put
  1065.         things into the environment for a single command run:
  1066.  
  1067.         name=felix age=56 awk '... ENVIRON["name"] .....'
  1068.  
  1069.         this also works with ksh and some other shells.
  1070.  
  1071. The first approach is extremely portable, but doesn't work with
  1072. awk "-f" script files.  In that case, it's better to use a shell
  1073. script and stretch a long awk command argument in '...' across
  1074. multiple lines if need be.
  1075.  
  1076. Also note: /bin/csh requires a \ before an embedded newline, /bin/sh not.
  1077.  
  1078.  
  1079. 14.2 Unix Shell Quoting
  1080.  
  1081. Quoting can be such a headache for the novice, in shell programming,
  1082. and especially in awk.
  1083.  
  1084. Art Povelones posted a long tutorial on shell quoting on 1999/09/30
  1085. which is probably too much detail to repeat with the FAQ; if you
  1086. could use it, search via <http://groups.google.com/>.
  1087.  
  1088. Tim Maher offered his <http://www.consultix-inc.com/quoting.txt>.
  1089.  
  1090. This approach is probably the best, and easiest to understand and
  1091. maintain, for most purposes:  (the '@@' is quoted to ensure the
  1092. shell will copy verbatim, not interpreting environment variable
  1093. substitutions etc.)
  1094.  
  1095.     cat <<'@@' > /tmp/never$$.awk
  1096.     { print "Never say can't." }
  1097.     @@
  1098.     awk -f /tmp/never$$.awk; rm /tmp/never$$.awk
  1099.  
  1100. If you enjoy testing your shell's quoting behavior frequently, you
  1101. could try these:
  1102.  
  1103.     (see below for a verbose explanation of the first one, with 7 quotes)
  1104.  
  1105.     awk 'BEGIN { q="'"'"'";print "Never say can"q"t."; exit }'
  1106.     nawk -v q="'" 'BEGIN { print "Never say can"q"t."; exit }'
  1107.     awk 'BEGIN { q=sprintf("%c",39); print "Never say can"q"t."; exit }'
  1108.     awk 'BEGIN { q=sprintf("%c",39); print "Never say \"can"q"t.\""; exit }'
  1109.  
  1110. However, you would also have to know why you could not use this:
  1111.  
  1112.     awk 'BEGIN { q="\'"; print "Never say \"can"q"t.\""; exit }'
  1113.  
  1114. explanation of the 7-quote example:
  1115.  
  1116. note that it is quoted three different ways:
  1117.  
  1118.     awk 'BEGIN { q="'
  1119.                      "'"
  1120.                         '";print "Never say can"q"t."; exit }'
  1121.  
  1122. and that argument comes out as the single string (with embedded spaces)
  1123.  
  1124.     BEGIN { q="'";print "Never say can"q"t."; exit }
  1125.  
  1126. which is the same as
  1127.  
  1128.     BEGIN { q="'"; print "Never say can" q "t."; exit }
  1129.                           ^^^^^^^^^^^^^  ^  ^^
  1130.                           |           |  |  ||
  1131.                           |           |  |  ||
  1132.                           vvvvvvvvvvvvv  |  ||
  1133.                           Never say can  v  ||
  1134.                                          '  vv
  1135.                                             t.
  1136.  
  1137. which, quite possibly with too much effort to be worth it, gets you
  1138.  
  1139.                           Never say can't.
  1140.  
  1141.  
  1142. 14.3 ENVIRON[] and "env"|getline
  1143.  
  1144.    Modern versions of new awk (gawk, mawk, Bell Labs awk, any POSIX
  1145.    awk) all provide an array named ENVIRON.  The array is indexed by
  1146.    environment variable name; the value is that variable's value.
  1147.    For instance, ENVIRON["HOME"] might be "/home/chris".  To print
  1148.    out all the names and values, use a simple loop:
  1149.  
  1150.         for (i in ENVIRON)
  1151.                 printf("ENVIRON['%s'] = '%s'\n", i, ENVIRON[i])
  1152.  
  1153.    What if my awk doesn't have ENVIRON[]?
  1154.  
  1155.    Short answer, get a better awk.  There are many freely available
  1156.    versions.
  1157.  
  1158.    Longer answer, on Unix you can use a pipe from the `env' or
  1159.    `printenv' commands, but this is less pretty, and may be a
  1160.    problem if the values contain newlines:
  1161.  
  1162.         # test this on your system before you depend on it!
  1163.         while ( ("env" | getline line) >0 )
  1164.         {
  1165.                 varname=line
  1166.                 varvalue=line
  1167.                 sub(/=.*$/,"",varname)
  1168.                 sub(/^[^=]*=/,"",varvalue)
  1169.                 print "var [" varname "]='" varvalue "'"
  1170.         }
  1171.  
  1172. 14.4 exporting environment variables back to the parent process
  1173.  
  1174.    How can I put values into the environment of the program that
  1175.    called my awk program?
  1176.  
  1177.    Short answer, you can't.  Unix ain't Plan 9, and you can't tweak
  1178.    the parent's address space.
  1179.  
  1180.    (DOS isn't even Unix, so it lets any program overwrite any memory
  1181.    location, including the parent's environment space.  But the
  1182.    details are [obviously] going to be fairly icky.  Avoid.)
  1183.  
  1184.    Longer answer, write the results in a form the shell can parse
  1185.    to a temporary file, and have the shell "source" the file after
  1186.    running the awk program:
  1187.  
  1188.         awk 'BEGIN { printf("NEWVAR='%s'\n", somevalue) }' > /tmp/awk.$$
  1189.         . /tmp/awk.$$        # sh/ksh/bash/pdksh/zsh etc
  1190.         rm /tmp/awk.$$
  1191.  
  1192.    With many shells, you can use `eval', but this is also cumbersome:
  1193.  
  1194.         eval `awk 'BEGIN { print "NEWVAR=" somevalue }'`
  1195.  
  1196.    Csh syntax and more robust use of quotation marks are left as
  1197.    exercises for the reader.
  1198.  
  1199.  
  1200. ========================================================================
  1201.  
  1202. 15. Is there an easy way to determine if you have oawk or nawk?
  1203.  
  1204. The following in a BEGIN rule will do the trick.
  1205.  
  1206.         if (ARGC == 0)
  1207.                 # old awk
  1208.         else
  1209.                 # new awk
  1210.  
  1211.  
  1212. ========================================================================
  1213.  
  1214. 16. How does awk deal with multiple files?
  1215.  
  1216.   16.0 Version warning
  1217.  
  1218.     some of these techniques will require non-ancient versions of awk.
  1219.  
  1220.   16.1 How can awk test for the existence of a file?
  1221.  
  1222.     the most portable way is to simply try and read from the file.
  1223.  
  1224.         function exists(file,        dummy, ret)
  1225.         {
  1226.                 ret=0;
  1227.                 if ( (getline dummy < file) >=0 )
  1228.                 {
  1229.                         # file exists (possibly empty) and can be read
  1230.                         ret = 1;
  1231.                         close(file);
  1232.                 }
  1233.                 return ret;
  1234.         }
  1235.  
  1236. [ I've read reports that earlier versions of mawk would write to stderr
  1237. as well as getline returning <0 -- is this still true? ]
  1238.  
  1239.         on Unix, you can probably use the `test' utility
  1240.  
  1241.         if (system("test -r " file) == 0)
  1242.             # file is readable
  1243.         else
  1244.             # file is not readable
  1245.  
  1246.   16.2 How can I get awk to read multiple files?
  1247.  
  1248.     it's automatic (under Unix at least) -- use something like:
  1249.  
  1250.     awk '/^#include/ {print $2}' *.c *.h
  1251.  
  1252.   16.3 How can I tell from which file my input is coming?
  1253.  
  1254.     use the built-in variable FILENAME:
  1255.  
  1256.     awk '/^#include/ {print FILENAME,$2}' *.c *.h
  1257.  
  1258.   16.4 How can I get awk to open multiple files (selected at runtime)?
  1259.  
  1260.     use `getline', `close', and `print EXPR > FILENAME', like:
  1261.  
  1262.     # assumes input file has at least 1 line, output file writeable
  1263.     function double(infilename,outfilename,    aline)
  1264.     {
  1265.       while ( (getline aline < infilename) >0 )
  1266.         print(aline aline) > outfilename;
  1267.       close(infilename);
  1268.       close(outilename);
  1269.     }
  1270.  
  1271.   16.5 How can I treat the first file specially?
  1272.  
  1273.     use FILENAME, thusly:
  1274.  
  1275.     BEGIN { rulesfile="" }
  1276.     rulesfile == "" { rulesfile = FILENAME; }
  1277.     FILENAME == rulesfile { build_rule($0); }
  1278.     FILENAME != rulesfile { apply_rule($0); }
  1279.  
  1280.     Example:  
  1281.  
  1282.     Suppose you have a text-line "database" and you want to make some
  1283.     batch changes to it, by replacing some old lines with new lines.
  1284.  
  1285.     BEGIN { rulesfile="" }
  1286.     rulesfile == "" { rulesfile = FILENAME; }
  1287.     rulesfile == FILENAME { replace[$1] = $0; }
  1288.     rulesfile != FILENAME \
  1289.     { 
  1290.             if ($1 in replace) 
  1291.                     print replace[$1];
  1292.             else
  1293.                     print;
  1294.     }
  1295.  
  1296.     another way, using ARGV:
  1297.  
  1298.     (FILENAME == ARGV[1]) { replace[$1] = $0; next }
  1299.     ($1 in replace) { print replace[$1]; next }
  1300.     { print }
  1301.  
  1302.   16.6 How can I explicitly pass in a filename to treat specially?
  1303.  
  1304.     use `-v rulesfile=filename' like you would any other variable,
  1305.     and then use a `getline' loop (and `close') in your BEGIN
  1306.     statement.
  1307.  
  1308.     BEGIN \
  1309.     {
  1310.       if (rulesfile=="")
  1311.       {
  1312.         print "must use -v rulesfile=filename";
  1313.         exit(1);
  1314.       }
  1315.       while ( (getline < rulesfile) >0 )
  1316.         replace[$1]=$0;
  1317.       close(rulesfile);
  1318.     }
  1319.  
  1320.     {
  1321.       if ($1 in replace)
  1322.         print replace[$1];
  1323.       else
  1324.         print;
  1325.     }
  1326.  
  1327.  
  1328. ========================================================================
  1329.  
  1330. 17. How many elements were created by split()?
  1331.  
  1332.    when I do a split on a field, e.g.,
  1333.  
  1334.         split($1,x,"string")
  1335.  
  1336.    how can i find out how many elements x has (I mean other than
  1337.    testing for null string or doing a `for (n in x)' test)?
  1338.  
  1339. split() is a function; use its return value:
  1340.  
  1341.         n = split($1, x, "string")
  1342.  
  1343.  
  1344. ========================================================================
  1345.  
  1346. 18. How can I split a string into characters?
  1347.  
  1348. in portable POSIX awk, the only way to do this is to use substr to pull
  1349. out each character, one by one.  this is painful.  however, gawk, mawk,
  1350. and the newest version of the Bell Labs awk all allow you to set
  1351. FS = "" and use "" as the third argument of split.
  1352.  
  1353. so, split("chars",anarray,"") results in the array anarray containing
  1354. 5 elements -- "c", "h", "a", "r", "s".
  1355.  
  1356. if you don't have any ^As in your string, you could try:
  1357.  
  1358.         string=$0;
  1359.         gsub(".", "&\001", string)
  1360.         n=split(string, anarray, "\001")
  1361.         for (i=1;i<=n;i++)
  1362.             print "character " i "is '" anarray[i] "'";
  1363.  
  1364.  
  1365. ========================================================================
  1366.  
  1367. 19. Why does SunOS/Solaris awk behave oddly?
  1368.  
  1369. I want to use the tolower() function with SunOS nawk, but all I get is
  1370.  
  1371.         nawk: calling undefined function tolower
  1372.  
  1373. The SunOS nawk is from a time before awk acquired the tolower() and
  1374. toupper() functions.  Either use one of the freely available awks, or
  1375. or use /usr/xpg4/bin/awk (if you have it), or write your own function
  1376. to do it using index, substr, and gsub.
  1377.  
  1378. An example of such a function is in O'Reilly's _Sed & Awk_.
  1379.  
  1380. Patrick TJ McPhee writes:
  1381.  
  1382. > SunOS includes three versions of awk. /usr/bin/awk is the old
  1383. > (pre-1989) version. /usr/bin/nawk is the new awk which appeared
  1384. > in 1989, and /usr/xpg4/bin/awk is supposed to conform to the single
  1385. > unix specification. No one knows why Sun continues to ship old awk.
  1386.  
  1387.  
  1388. ========================================================================
  1389.  
  1390. 20. How do I have dynamic-width printf strings, like C?
  1391.  
  1392. with modern awks, you can just do it like you would in C (though the
  1393. justification is less clear; C doesn't have the trivial in-line string
  1394. concatenation that awk does), like so:
  1395.  
  1396.         maxlen=0
  1397.  
  1398.         for (i in arr)
  1399.           if (maxlen<length(arr[i]))
  1400.             maxlen=length(arr[i])
  1401.  
  1402.         for (i in arr)
  1403.           printf("%-*s %s\n",maxlen,arr[i],i)
  1404.  
  1405. with old awks, just do it like you would do if you didn't know about %*
  1406. (this would be much more painful to do in C), like so:
  1407.  
  1408.         maxlen=0
  1409.  
  1410.         for (i in arr)
  1411.           if (maxlen<length(arr[i]))
  1412.             maxlen=length(arr[i])
  1413.  
  1414.         printfstring="%-" maxlen "s %s\n";
  1415.         for (i in arr)
  1416.           printf(printfstring,arr[i],i)
  1417.  
  1418.  
  1419. ========================================================================
  1420.  
  1421. 21. Why doesn't "\\$" behave like /\\$/ ?  Why don't parentheses match?
  1422.  
  1423. because "\\$" is a string and /\\$/ is not; in strings, some of the
  1424. escape characters get eaten up (like \" to escape a double-quote within
  1425. the string).
  1426.  
  1427. /\\$/ => regular expression:  literal backslash at end-of-expression
  1428.  
  1429. "\\$" => string: \$ => regular expression:  literal dollar sign
  1430.  
  1431. to get behavior like the first case in a string, use "\\\\$" .
  1432.  
  1433. there are other, less obvious characters which need the same attention;
  1434. under-quoting or over-quoting should be avoided:
  1435.  
  1436. parentheses are special for alternation:
  1437.  
  1438. /\(test\)/ => 6 characters `(test)'
  1439. "\(test\)" => /(test)/ => 4 characters `test' (with unused grouping)
  1440.  
  1441. an example of trying to match some diagonal compass directions:
  1442.  
  1443. /(N|S)(E|W)/ => `NE' or `NW' or `SE' or `SW' (correct)
  1444. "(N|S)(E|W)" => /(N|S)(E|W)/ (correct)
  1445. "\(N|S\)\(E|W\)" => /(N|S)(E|W)/ (correct) (NOTE:  all \ had no effect)
  1446. "\(N\|S\)\(E\|W\)" => /(N|S)(E|W)/ (correct) (NOTE:  all \ had no effect)
  1447.  
  1448. expressions that look similar but behave totally differently:
  1449.  
  1450. /\(N|S\)\(E|W\)/ => `(N' or `S)(E' or `W)'
  1451. /\(N\|S\)\(E\|W\)/ => `(N|S)(E|W)' only
  1452.  
  1453. There is also confusion regarding different forms of special characters;
  1454. POSIX requires that `\052' be treated as any other `*', even though it
  1455. is written with 4 bytes instead of 1.  In compatibility mode, gawk will
  1456. treat it as though it were escaped , namely `\*'.
  1457.  
  1458.  
  1459. ========================================================================
  1460.  
  1461. 22. What is awk's exit code?
  1462.  
  1463. normally, the `exit' command exits with a value of zero.
  1464.  
  1465. you can supply an optional numeric value to the `exit' command to
  1466. make it exit with a value:
  1467.  
  1468.     if (whatever)
  1469.         exit 12;
  1470.  
  1471. if you have an END block, control first transfers there.  within
  1472. the END block, an `exit' command exits immediately; if you had
  1473. previously supplied a value, that value is used.  but, if you
  1474. give a new value to `exit' within the END block, the new value is
  1475. used.  this is documented in the GNU Awk User's Guide (gawk.texi).
  1476.  
  1477. if you have an END block you want to be able to skip sometimes,
  1478. you may have to do something like this:
  1479.  
  1480. BEGIN \
  1481. {
  1482.   exitcode=0;
  1483.   ...
  1484. }
  1485.  
  1486. # normal rules processing...
  1487. {
  1488.   ...
  1489.   if (fatal)
  1490.   {
  1491.     exitcode=12;
  1492.     exit(exitcode);
  1493.   }
  1494.   ...
  1495. }
  1496.  
  1497. END {
  1498.   if (exitcode!=0)
  1499.     exit(exitcode);
  1500.   ...
  1501. }
  1502.  
  1503.  
  1504. ========================================================================
  1505.  
  1506. 23. How can I get awk to be case-insensitive?
  1507.  
  1508.   23.1. use tolower()
  1509.     - portable
  1510.     - must be explicitly used for each comparison
  1511.  
  1512.     instead of:
  1513.       if (avar=="a" || avar=="A") { ... }
  1514.     use:
  1515.       if (tolower(avar)=="a") { ... }
  1516.  
  1517.     or at the beginning of your code, add a line like
  1518.       { for (i=0;i<=NF;i++) $i=tolower($i) }
  1519.       { $0=tolower($0); }   # modern awks will rebuild $1..$NF also
  1520.  
  1521.   23.2. use IGNORECASE=1;
  1522.     - gawk only
  1523.     - used for all comparisons, regex comparisons, index() function
  1524.     - not used for array indexing
  1525.  
  1526.  
  1527. ========================================================================
  1528.  
  1529. 24. How can I force a numeric/non-numeric comparison?
  1530.  
  1531. these are the canonical, work-in-all-versions snippets.  there are
  1532. many others, most longer, some shorter (but possibly less portable).
  1533.  
  1534. to compare two variables as numbers ONLY, use
  1535.   if (0+var1 == 0+var2)
  1536.  
  1537. to compare two variables as non-numeric strings ONLY, use
  1538.   if ("" var1 == "" var2)
  1539.  
  1540.  
  1541. ========================================================================
  1542.  
  1543. 25. Why does { FS=":"; print $1 } not split the first record?
  1544.  
  1545. basically, you should set FS before it may be called upon to split $0
  1546. into fields.  once awk encounters a `{', it is probably too late.
  1547.  
  1548. some awk implementations set the fields at the beginning of the
  1549. block, and don't re-parse just because you changed FS.  to get
  1550. the desired behavior, you must set FS _before_ reading in a line.
  1551.  
  1552. e.g.,
  1553.   BEGIN { FS=":" }
  1554.   { print $1 }
  1555.  
  1556. e.g.,
  1557.   awk -F: '{ print $1 }'
  1558.  
  1559. if you run code like this
  1560.   { FS=":"; print $1 }
  1561.  
  1562. on this data:
  1563.   first:second:third but not last:fourth
  1564.   First:Second:Third But Not Last:Fourth
  1565.   FIRST:SECOND:THIRD BUT NOT LAST:FOURTH
  1566.  
  1567. you may get either
  1568.   this:       or this:
  1569.   ----        -------
  1570.   first       first:second:third
  1571.   First       First
  1572.   FIRST       FIRST
  1573.  
  1574. perhaps more surprisingly, code like
  1575.   { FS=":"; }
  1576.   { print $1; }
  1577.  
  1578. will also behave in the same way.
  1579.  
  1580.  
  1581. ========================================================================
  1582.  
  1583. 26. Did ^ and $ and . change in gawk?
  1584.  
  1585. yes.  early versions cared about \n (newlines) and treated them
  1586. specially.  version 3.* and later are more POSIX-compliant here.
  1587.  
  1588.  
  1589. ========================================================================
  1590.  
  1591. 27. Why doesn't awk 'begin {...}' work?
  1592.  
  1593. it needs to be `BEGIN' (i.e., it's case-sensitive).
  1594.  
  1595.  
  1596. ========================================================================
  1597.  
  1598. 28. Why does awk 'BEGIN { print 6 " " -22 }' lose the space?
  1599.  
  1600. You'd expect `6 -22', but you get `6-22'.  It's because the `" " -22'
  1601. is grouped first, as a substraction instead of a concatenation, resulting
  1602. in the numeric value `-22'; then it is concatenated with `6', giving the
  1603. string `6-22'.  Gentle application of parentheses will avoid this.
  1604.  
  1605.  
  1606. ========================================================================
  1607.  
  1608. 29. How do I take advantage of gawk's networking support?
  1609.  
  1610. (I expect this will become removed from the FAQ as people become more
  1611. aware of the new gawk documentation covering this.  But it's new and
  1612. cool and deserves attention.)
  1613.  
  1614. Network interfacing in gawk is easier than in many other languages.
  1615. Networking connections are made available like files in gawk.  You can
  1616. open, read, write and close them.  This script opens a connection to
  1617. the machine you are working on (localhost).  Then it reads the answer
  1618. and closes the connection.
  1619.  
  1620.     BEGIN \
  1621.     {
  1622.       NetService = "/inet/tcp/0/localhost/finger";
  1623.       print "name" |& NetService;
  1624.       while ((NetService |& getline) > 0)
  1625.         print $0;
  1626.       close(NetService);
  1627.     }
  1628.  
  1629. It tells you which users are currently logged into your system.
  1630. Note the similarity to ordinary file reading.
  1631.  
  1632. With gawk 3.1.0, this networking support can not be used on
  1633. Microsoft Windows unless you are using the Cygwin environment.
  1634. If you have Cygwin installed, configure and compile gawk as if you
  1635. were using a Unix system:
  1636.  
  1637.    tar -xvpzf gawk-3.1.x.tar.gz   # x is patch number
  1638.    cd gawk-3.1.x
  1639.    ./configure && make
  1640.  
  1641. The configure step takes a llloooonnngggg time.
  1642.  
  1643.  
  1644. ========================================================================
  1645.  
  1646. 98. Miscellaneous
  1647.  
  1648.  
  1649. ========================================================================
  1650.  
  1651. 99. Credits
  1652.  
  1653. I expect most of the information in this FAQ to be supplied by people
  1654. other than myself -- it's just going to work better that way.  The
  1655. newsgroup readers have a LOT more awk experience than I ever will
  1656. (unless I multiply myself by a few thousand, which is not legal with
  1657. today's tax laws).
  1658.  
  1659.  
  1660. These people have contributed to the well-being of the FAQ:
  1661.  
  1662.   arnold [at] skeeve.com (Arnold D. Robbins)
  1663.   walkerj [at] compuserve.com (James G. Walker)
  1664.   jland [at] worldnet.att.net (Jim Land)
  1665.   yuli.barcohen [at] telrad.co.il (Yuli Barcohen)
  1666.   johnd [at] mozart.inet.co.th (John DeHaven)
  1667.   amnonc [at] mercury.co.il (Amnon Cohen)
  1668.   saguyami [at] post.tau.ac.il (Shay)
  1669.   hankedr [at] mail.auburn.edu (Darrel Hankerson)
  1670.   mark [at] ispc001.demon.co.uk (Mark Katz)
  1671.   brennan [at] whidbey.com (Michael D. Brennan)
  1672.   neitzel [at] gaertner.de (Martin Neitzel)
  1673.   pjf [at] osiris.cs.uoguelph.ca (Peter Jaspers-Fayer)
  1674.   dmckeon [at] swcp.com (Denis McKeon)
  1675.   neil_mahoney [at] il.us.swissbank.com (Neil Mahoney)
  1676.   dzubera [at] CS.ColoState.EDU (Zube)
  1677.   allen [at] gateway.grumman.com (John L. Allen)
  1678.   jerabek [at] rm6208.gud.siemens.co.at (Martin Jerabek)
  1679.   thull [at] ocston.org (Tom Hull)
  1680.   bmarcum [at] iglou.com (Bill Marcum)
  1681.   thobe [at] lafn.org (Glenn Thobe) 
  1682.   boffi [at] rachele.stru.polimi.it (giacomo boffi)
  1683.   hastinga [at] tarim.dialogic.com (Austin Hastings)
  1684.   konrad [at] netcom.com (Konrad Hambrick)
  1685.   jmccann [at] WOLFENET.com (James McCann)
  1686.   eia018 [at] comp.lancs.ac.uk (Dr Andrew Wilson)
  1687.   Alex.Schoenmakers [at] lhs.be
  1688.   rwab1 [at] cl.cam.ac.uk (Ralph Becket)
  1689.   jesusmc [at] scripps.edu (Jesus M. Castagnetto)
  1690.   monty [at] primenet.com (Jim Monty)
  1691.   epement [at] ripco.com (Eric Pement)
  1692.   gavin [at] wraith.u-net.com (Gavin Wraith)
  1693.   pierre [at] mail.asianet.it (Gianni Rondinini)
  1694.   lothar [at] u-aizu.ac.jp (Lothar M. Schmitt)
  1695.   morrisl [at] scn.org (Larry D. Morris)
  1696.   Juergen.Kahrs [at] t-online.de
  1697.   kahrs [at] iSenseIt.de (Juergen Kahrs)
  1698.   tim [at] consultix-inc.com (Tim Maher/CONSULTIX)
  1699.   phil [at] bolthole.com (Philip Brown)
  1700.   andrew_sumner [at] bigfoot.com (Andrew Sumner)
  1701.   jblaine [at] shore.net (Jeff Blaine)
  1702.   dmeier.esperanto [at] gmx.de (Detlef Meier)
  1703.   heiner.steven [at] nexgo.de (Heiner Steven)
  1704.   joe [at] plaguesplace.dyndns.org
  1705.   hstein [at] airmail.net (Harry Stein)
  1706.   ptjm [at] interlog.com (Patrick TJ McPhee)
  1707.   db21 [at] ih4ess.ih.lucent.com (David Beyerl)
  1708.   art [at] pove.com (Art Povelones)
  1709.   jari.aalto [at] ntc.nokia.com (Jari Aalto)
  1710.   jlaiho [at] ichaos.nullnet.fi (Juha Laiho)
  1711.   walter [at] wbriscoe.demon.co.uk (Walter Briscoe)
  1712.   SimonN [at] draeger.com (Nicole Simon)
  1713.   peter.tillier [at] btinternet.com (Peter S Tillier)
  1714.   churchyh [at] ccwf.cc.utexas.edu (Henry Churchyard)
  1715.   Ferran.Jorba [at] uab.es (Ferran Jorba)
  1716.   Kalle.Tuulos [at] nmp.nokia.com (Kalle Tuulos)
  1717.   rms [at] friko.onet.pl (Rafal Sulejman)
  1718.   pjfarley [at] banet.net (Peter J. Farley III)
  1719.   neel [at] gnu.org
  1720.   afu [at] wta.att.ne.jp
  1721.   pez68 [at] netscape.net (Peter Stromberg)
  1722.   edgar.j.ramirez [at] lmco.com (Edgar J. Ramirez)
  1723.   pholzleitner [at] unido.org (Peter HOLZLEITNER)
  1724.   bps03z [at] email.mot.com (Peter Saffrey)
  1725.   jidanni [at] kimo.com.tw (Dan Jacobson)
  1726.   lehalle [at] earthling.net (Charles-Albert Lehalle)
  1727.   robin.moffatt [at] ntlworld.com (Robin Moffatt)
  1728.   markus [at] biewer.com (Markus B. Biewer)
  1729.   vincent [at] delau.nl (Vincent de Lau)
  1730.   vjpnreddy [at] hotmail.com (Jaya Reddy)
  1731.   David.Billinghurst [at] riotinto.com (David Billinghurst)
  1732.   j-korsv [at] online.no (Jon-Egil Korsvold)
  1733.  
  1734. Thanks.
  1735.  
  1736. ========================================================================
  1737.  
  1738. thus endeth the awk FAQ.
  1739.  
  1740.