home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / question / 10521 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.1 KB  |  75 lines

  1. Path: sparky!uunet!mcsun!Germany.EU.net!math.fu-berlin.de!Sirius.dfn.de!darwin.sura.net!wupost!sdd.hp.com!think.com!news.bbn.com!news.bbn.com!news
  2. From: cjross@bbn.com (chris ross)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: awk problems
  5. Date: 30 Aug 1992 04:19:41 GMT
  6. Lines: 64
  7. Message-ID: <la0j2tINNrtj@news.bbn.com>
  8. References: <32213@adm.brl.mil>
  9. NNTP-Posting-Host: bbn.com
  10.  
  11. CHUCK@bkmain.poly-east-london.ac.uk (Chuck Foster) writes:
  12.  
  13. >Is there any way to convert a string to lowercase/uppercase in awk scripts?
  14.  
  15. gawk has toupper() and tolower().  In nawk and awk you're forced to do
  16. something supremely inelegant.  (And in awk, you can't even define
  17. separate functions.)
  18.  
  19. BEGIN {
  20.     lowers = "abcdefghijklmnopqrstuvwxyz";
  21.     uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  22. }
  23.  
  24. function toupper (s) {
  25.     new_s = "";
  26.     len = length(s);
  27.     for ( i = 1; i <= len; i++ )
  28.     {
  29.     c = substr(s, i, 1);
  30.     if ((idx = index(lowers, c)) != 0)
  31.         new_s = new_s substr(uppers, idx, 1);
  32.     else
  33.         new_s = new_s c;
  34.     }
  35.     return(new_s);
  36. }
  37.  
  38. function tolower (s) {
  39.     new_s = "";
  40.     len = length(s);
  41.     for ( i = 1; i <= len; i++ )
  42.     {
  43.     c = substr(s, i, 1);
  44.     if ((idx = index(uppers, c)) != 0)
  45.         new_s = new_s substr(lowers, idx, 1);
  46.     else
  47.         new_s = new_s c;
  48.     }
  49.     return(new_s);
  50. }
  51.  
  52.  
  53. >I've tried   x = strupr($1);  but this just seems to return a string as is.
  54.  
  55. Not surprising.  Since strupr isn't defined as a function, awk reads
  56. this as two adjacent expressions and performs string concatenation.
  57. If strupr isn't set, this yields $1.
  58.  
  59. >Also, is it possible to find the offset of a token in the whole string? The
  60. >file I am operating upon is in fixed format, and a user's full name occurs
  61. >after column 68.
  62.  
  63. Try substr($0, 69).  With two args, substr reads from the specified
  64. offset (1-based) to the end of the string.
  65.  
  66. >Forgive my ignorance; I've only just begun to experiment with this
  67. >interesting command ... :-)
  68.  
  69. When you're about to tear your hair out, learn perl.  :-)
  70.  
  71.  
  72. chris ross   <cjross@bbn.com>   BBN Advanced Simulations   (617) 873-3272
  73.  WARNING: article may contain flammable material.  Do not expose to open
  74.    flames.  In case of accidental ignition, douse keyboard with water.
  75.