Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!ut-sally!husc6!cmcl2!beta!dph From: dph@beta.UUCP (David P Huelsbeck) Newsgroups: comp.unix.questions Subject: Re: awk question Message-ID: <7697@beta.UUCP> Date: Mon, 20-Jul-87 11:24:15 EDT Article-I.D.: beta.7697 Posted: Mon Jul 20 11:24:15 1987 Date-Received: Tue, 21-Jul-87 04:48:15 EDT References: <371@cos.COM> Reply-To: dph@beta.UUCP (David P Huelsbeck) Followup-To: Article 4551 of comp.unix.questions Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 47 Keywords: arrays, control flow Summary: array[0] causes awk strangeness You're obviously not a beginning awk programmer as you were not far off on this one. I don't remember running into this before but our 4.3 awk seems to get hosed up when I try to use array[0]. I assume this was half of your problem. Also if you store into an array using post-increment the final value of your index is 1 greater than the index of the last valid element. I'm sure this would have been easy to spot if the array[0] problem hadn't been getting you. The following does what you wanted with just a few changes to your script. Hope this helps. Sorry for posting this but my UUCP connection seems a bit flaky lately. [mitch, did you ever get my mail from last week ? ] David Huelsbeck dph@lanl.gov {cmcl2,ihnp4}!lanl!dph ---cut here--------cut here-------cut here--------cut here---------cut here--- BEGIN { i = 0 # not really needed but looks good to pascal types ;-) nip = 0; ntxt = 0 print "INIT foo" > "foo" } $1 !~ /\.IP/ { ++ntxt line[++i] = $0 # if pre-increment is used "i" is always a valid } # array element; just skip line[0] as it is not a # valid array location; THIS WAS YOUR PROBLEM $1 ~/\.IP/ { ++nip print > "foo" # send $0 -the .IP line- to foo for (j=1; j<=i; j++) { print line[j] > "foo" } i = 0 # reset i } END { printf "nip=%d\tntxt=%d\n", nip, ntxt > "foo" }