home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / alt / lang / awk / 28 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.9 KB  |  72 lines

  1. Path: sparky!uunet!UB.com!pacbell.com!sgiblab!spool.mu.edu!yale.edu!ira.uka.de!rz.uni-karlsruhe.de!fg70.rz.uni-karlsruhe.de!ig25
  2. From: ig25@fg70.rz.uni-karlsruhe.de (Thomas Koenig)
  3. Newsgroups: alt.lang.awk
  4. Subject: Re: tolower() function?
  5. Date: 28 Jan 93 13:16:55 GMT
  6. Organization: University of Karlsruhe, Germany
  7. Lines: 60
  8. Message-ID: <ig25.728227015@fg70>
  9. References: <1993Jan19.152725.3510@trentu.ca> <EMCOOP.93Jan19111755@bcars148.bnr.ca> <C184vr.p5q@austin.ibm.com>
  10. NNTP-Posting-Host: fg70.rz.uni-karlsruhe.de
  11.  
  12. tbates@austin.ibm.com () writes:
  13.  
  14.  
  15. >Quoting "The AWK Programming Language",
  16.  
  17. >    "The cleanest way to do case conversion in awk is with an array that
  18. >maps each letter...it's better to use...tr"
  19.  
  20. It certainly is cleaner, but not every system which has awk also has
  21. tr.
  22.  
  23. For the heck of it, I timed converting a 200 K text file to all upper
  24. with an awk - script with self - written toup - function,and tr, on a HP
  25. 9000/720 (not exactly a slow machine :-)
  26.  
  27. Here are the results:
  28.  
  29. $ time tr [[:lower:]] [[:upper:]] < faq.linux >/dev/null
  30.  
  31. real    0m0.33s
  32. user    0m0.16s
  33. sys     0m0.07s
  34.  
  35. $ time toupper < faq.linux >/dev/null
  36.  
  37. real    0m33.32s
  38. user    0m32.12s
  39. sys     0m0.07s
  40.  
  41. $ cat toupper
  42. #! /usr/bin/awk -f
  43. BEGIN    {
  44.         a="abcdefghijklmnopqrstuvwxyz"; b="ABCDEFGHIJKLMNOPQRSTUWVXYZ"
  45.         for (i=1; i<=length(a); i++)
  46.             touptab[substr(a,i,1)] = substr(b,i,1)
  47.     }
  48.     { print toup($0) }
  49. function toup(a     ,i ,ch ,res)
  50. {
  51.     res = ""
  52.     for (i=1; i<=length(a); i++)
  53.     {
  54.     ch = substr(a,i,1)
  55.     if (ch in touptab)
  56.         res = res touptab[ch]
  57.     else
  58.         res = res ch
  59.     }
  60. return res
  61. }
  62.  
  63. Which again shows...  leave the stuff which can be handeled by more
  64. specialized utilities to these, and only use awk when you really need
  65. it.
  66.  
  67. Regards
  68. -- 
  69. Thomas Koenig, ig25@rz.uni-karlsruhe.de, ig25@dkauni2.bitnet
  70. The joy of engineering is to find a straight line on a double
  71. logarithmic diagram.
  72.