home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- 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
- From: tmiller@debug.cuc.ab.ca (Trever Miller)
- Subject: Re: More fun with awk! (not associative arrays, I promise. ;-)
- Organization: Debug Computer Services
- Date: Wed, 26 Aug 92 00:50:39 GMT
- Message-ID: <1992Aug26.005039.12201@debug.cuc.ab.ca>
- Lines: 83
-
- >
- >This isn't what I wanted! There can be ANY number of blocks of text
- >and each block can be ANY size. Just so I can ramble further, let
- >me make it VERY clear with another example:
- >
- >I have this file:
- >
- >...
- >[2] Canada North America
- >[3] China Asia
- >[4] Argentina South America
- >...
- >[6] United States North America
- >[7] Scotland Europe
- >[8] Australia Australia
- >[9] Chad Africa
- >[10] Brazil South America
- >...
- >[13] Mexico North America
- >[14] Thailand Asia
- >[15] Peru South America
- >
- >
- >I want to say:
- >
- >$ awk -f 1stblock testfile
- >(and get the results)
- >[2] Canada North America
- >[3] China Asia
- >[4] Argentina South America
- >
- >and
- >
- >$ awk -f lastblock testfile
- >(and get the results)
- >[13] Mexico North America
- >[14] Thailand Asia
- >[15] Peru South America
- >
- >I still can't figure out how to do this. And YES, I DO HAVE THE AWK BOOK!
- >
-
- OK, ok. Maybe I didn't read your first post correctly.
- Here's a new script for you to try :
-
- ** Script started Tue Aug 25 18:44:50 MDT 1992. File "typescript" **
-
- $ cat block.awk
- BEGIN {FS="\n";RS="...";block+=0}
- NF > 0 {record[i++]=$0}
- END {printf("block %d : %s\n",block,record[block])}
-
- $ awk -f block.awk datafile
- block 0 :
- [2] Canada North America
- [3] China Asia
- [4] Argentina South America
-
- $ awk -f block.awk block=2 datafile
- block 2 :
- [13] Mexico North America
- [14] Thailand Asia
- [15] Peru South America
-
- $ awk -f block.awk block=1 datafile
- block 1 :
- [6] United States North America
- [7] Scotland Europe
- [8] Australia Australia
- [9] Chad Africa
- [10] Brazil South America
-
- $ exit
-
- ** Script ended Tue Aug 25 18:45:47 MDT 1992. File "typescript" **
-
- The block+=0 coerces it to a numeric value which then will default to
- block=0 if none is given on the command line.
-
- --
- Internet : tmiller@debug.cuc.ab Magic BBS : "Ambush Bug"
- ambush.bug@debug.cuc.ab (403)569-2882 thru 2886
- trever%pixel@cpsc.ucalgary.ca
-