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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!usc!sol.ctr.columbia.edu!destroyer!ubc-cs!unixg.ubc.ca!kakwa.ucs.ualberta.ca!acs.ucalgary.ca!cpsc.ucalgary.ca!debug!tmiller
  3. From: tmiller@debug.cuc.ab.ca (Trever Miller)
  4. Subject: Re: More fun with awk! (not associative arrays, I promise. ;-)
  5. Organization: Debug Computer Services
  6. Date: Wed, 26 Aug 92 00:50:39 GMT
  7. Message-ID: <1992Aug26.005039.12201@debug.cuc.ab.ca>
  8. Lines: 83
  9.  
  10. >
  11. >This isn't what I wanted! There can be ANY number of blocks of text
  12. >and each block can be ANY size. Just so I can ramble further, let
  13. >me make it VERY clear with another example:
  14. >
  15. >I have this file:
  16. >
  17. >...
  18. >[2]      Canada         North America
  19. >[3]      China          Asia
  20. >[4]      Argentina      South America
  21. >...
  22. >[6]      United States  North America
  23. >[7]      Scotland       Europe
  24. >[8]      Australia      Australia
  25. >[9]      Chad           Africa
  26. >[10]     Brazil         South America
  27. >...
  28. >[13]     Mexico         North America
  29. >[14]     Thailand       Asia
  30. >[15]     Peru           South America
  31. >
  32. >
  33. >I want to say:
  34. >
  35. >$ awk -f 1stblock testfile
  36. >(and get the results)
  37. >[2]      Canada         North America
  38. >[3]      China          Asia
  39. >[4]      Argentina      South America
  40. >
  41. >and
  42. >
  43. >$ awk -f lastblock testfile
  44. >(and get the results)
  45. >[13]     Mexico         North America
  46. >[14]     Thailand       Asia
  47. >[15]     Peru           South America
  48. >
  49. >I still can't figure out how to do this. And YES, I DO HAVE THE AWK BOOK!
  50. >
  51.  
  52. OK, ok.  Maybe I didn't read your first post correctly. 
  53. Here's a new script for you to try :
  54.  
  55. ** Script started Tue Aug 25 18:44:50 MDT 1992.  File "typescript" **
  56.  
  57. $ cat block.awk
  58. BEGIN  {FS="\n";RS="...";block+=0}
  59. NF > 0 {record[i++]=$0}
  60. END    {printf("block %d : %s\n",block,record[block])}
  61.  
  62. $ awk -f block.awk datafile
  63. block 0 : 
  64. [2]      Canada         North America
  65. [3]      China          Asia
  66. [4]      Argentina      South America
  67.  
  68. $ awk -f block.awk block=2 datafile
  69. block 2 : 
  70. [13]     Mexico         North America
  71. [14]     Thailand       Asia
  72. [15]     Peru           South America
  73.  
  74. $ awk -f block.awk block=1 datafile
  75. block 1 : 
  76. [6]      United States  North America
  77. [7]      Scotland       Europe
  78. [8]      Australia      Australia
  79. [9]      Chad           Africa
  80. [10]     Brazil         South America
  81.  
  82. $ exit
  83.  
  84. ** Script ended Tue Aug 25 18:45:47 MDT 1992.  File "typescript" **
  85.  
  86. The block+=0 coerces it to a numeric value which then will default to
  87. block=0 if none is given on the command line.
  88.  
  89. -- 
  90. Internet : tmiller@debug.cuc.ab                 Magic BBS : "Ambush Bug"
  91.            ambush.bug@debug.cuc.ab              (403)569-2882 thru 2886
  92.            trever%pixel@cpsc.ucalgary.ca
  93.