EOF detection is not very reliable if there are multiple files on the command line. File start detection is more reliable.
For this, the first file is special, and we ignore FNR == 1.
After the first file, FNR == 1 becomes the end of the previous file. last_filename always has the name of the file you are processing.
Perform file processing after else.
Perform EOF processing inside the else block and in the END block.
gawk 'BEGIN{last_filename="";} \ FNR==1{if (last_filename==""){last_filename=FILENAME;} \ else {print "EOF: "last_filename;last_filename=FILENAME;}} \ END{print "END: "last_filename;}' $*
For multiple file sets, the else block is executed in EOF for all but the last file. The last file is executed in the END block.
For single sets of files, the else block is not executed, and the END block is executed.
rickfoosusa Feb 18 '14 at 21:44 2014-02-18 21:44
source share