End of file in AWK

Hello!

I need to control when i arrive to the end of my file, any built-in? or another way to do?

Thanks!

No built-in for EOF in awk, you can do something like this:

awk -v eof=$(wc -l < file) '
NR==eof{print "Last line: " $0}
' file

the so called "EOF" in awk is the END{} block or when getline returns 0. what do you want to "control" ?

# more file
1
2
3
$ awk 'BEGIN{ while(1) { if ( (getline line < "file") == 0 ) {print "eof: "line;break} } }'
eof: 3

You have the END special pattern. If more than one input file is processed you can either use the new experimental ENDFILE pattern (available with a separate patch for Gnu AWK) or write it yourself using the FILENAME built-in variable.

This link is also relevant.

The solution proposed for Franklin is good for me, because i can't use the END instruction, i need to know the end of file in the body of my awk.

Thanks!