bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gawk] ENDFILE bug?


From: Ed Morton
Subject: [bug-gawk] ENDFILE bug?
Date: Sat, 17 May 2014 13:59:35 -0500
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0

I think it's a bug to not allow getline to populate $0 in ENDFILE, and the error message when not using getline correctly in ENDFILE is misleading.

For comparison with END (the first END one is just printing the last $0 from the main loop, included here just to show that it doesn't fail for comparison with ENDFILE):

-------
$ awk 'END{ getline; print }' file1
file 1, line 2

$ awk 'END{ getline < "file2"; print }' file
file 2, line 1

$ awk 'END{ getline var;  print var }' file1


$ awk 'END{ getline var < "file2";  print var }' file1
file 2, line 1

--------
$ awk 'ENDFILE{ getline; print }' file1
awk: cmd. line:1: error: `getline' invalid inside `ENDFILE' rule

$ awk 'ENDFILE{ getline < "file2"; print }' file
awk: cmd. line:1: error: `getline' invalid inside `ENDFILE' rule

$ awk 'ENDFILE{ getline var; print var }' file1
awk: cmd. line:1: error: `getline var' invalid inside `ENDFILE' rule

$ awk 'ENDFILE{ getline var < "file2"; print var }' file
file 2, line 1

--------

So, despite what the error message says ( `getline' invalid inside `ENDFILE'), you CAN use a redirected getline in ENDFILE (and BEGINFILE) but only if using it to populate a variable rather than $0. END, on the other hand, lets you do either.

It's not obvious why ENDFILE would require a variable to populate when END doesn't, and I think that error message is misleading. A better error message might be something like:

error: `getline` usage requires `getline var < file' inside `ENDFILE' rule

Regards,

     Ed.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]