github.com/benhoyt/goawk@v1.8.1/testdata/gawk/redfilnm.awk (about) 1 #Date: Tue, 18 May 1999 12:48:07 -0500 (CDT) 2 #From: Darrel Hankerson <hankedr@dms.auburn.edu> 3 #To: arnold@gnu.org 4 #Subject: [christopher.procter@bt.com: RE: Getline bug in Gawk 3.0.3] 5 # 6 #Here's a reply that came directly to me. --darrel 7 # 8 # 9 #From: christopher.procter@bt.com 10 #To: hankedr@dms.auburn.edu 11 #Subject: RE: Getline bug in Gawk 3.0.3 12 #Date: Tue, 18 May 1999 18:42:28 +0100 13 # 14 #Sorry that was me getting carried away and cut and pasting the wrong thing 15 #into my email 16 # 17 #The real problem seems to be that : 18 #BEGIN { 19 #for (i=1;i<10;i++){ 20 # while((getline < "hello.txt")>0){ 21 # print $0 22 # } 23 # close("hello.txt") 24 # } 25 #} 26 #works (printing the contents of hello.txt 9 times), where as:- 27 # 28 #END{ 29 #for (i=1;i<10;i++){ 30 # while((getline < "hello.txt")>0){ 31 # print $0 32 # } 33 # close("hello.txt") 34 # } 35 #} 36 # 37 #doesn't, (it prints out hello.txt once followed by the iteration numbers 38 #from 1 to 9). 39 #The only difference is that one is in the BEGIN block and one in the END 40 #block. 41 # 42 #Sorry about the first post, I'm not a bad awk programmer, just a tired one 43 #:) 44 # 45 #chris 46 # 47 #> -----Original Message----- 48 #> From: Darrel Hankerson [SMTP:hankedr@dms.auburn.edu] 49 #> Sent: 18 May 1999 18:28 50 #> To: christopher.procter@bt.com 51 #> Subject: Re: Getline bug in Gawk 3.0.3 52 #> 53 #> Could you clarify? Your first script uses an apparently undefined 54 #> variable f. 55 #> 56 #> 57 #> christopher.procter@bt.com writes: 58 #> 59 #> BEGIN { 60 #> for (i=1;i<10;i++){ 61 #> while((getline < "hello.txt")>0){ 62 #> print $0 63 #> } 64 #> close(f) 65 #> } 66 #> } 67 #> 68 #> refuses to close the file and so prints the contents of hello.txt just 69 #> once. 70 #> However:- 71 #> 72 #> BEGIN { 73 #> f="hello.txt" 74 #> for (i=1;i<10;i++){ 75 #> while((getline < f)>0){ 76 #> print $0 77 #> } 78 #> close(f) 79 #> } 80 #> } 81 #> 82 #> works as advertised (printing the contents of hello.txt 9 times) 83 #> It seems like a bug in the close statement. 84 #> 85 #> -- 86 #> --Darrel Hankerson hankedr@mail.auburn.edu 87 # 88 89 # srcdir is assigned on command line --- ADR 90 END { 91 f = srcdir "/redfilnm.in" 92 for (i = 1; i < 10; i++){ 93 while((getline < f) > 0){ 94 print $0 95 } 96 close(f) 97 } 98 }