Is there a way to determine whether the current line is the last line of the input stream?
gawk implementation has special rule called ENDFILE
which will be triggered after processing every file in argument list. This works:
awk '{line=$0} ENDFILE {print line}' files...
more details you can find here>>
The special END
pattern will match only after the end of all input. Note that this pattern can't be combined with any other pattern.
More useful is probably the getline
pseudo-function which resets $0
to the next line and return 1, or in case of EOF return 0! Which I think is what you want.
For example:
awk '{ if(getline == 0) { print "Found EOF"} }'
If you are only processing one file, this would be equivalent:
awk 'END { print "Found EOF" }'
To detect the last line of each file in the argument list the following works nicely:
FNR == 1 || EOF {
print "last line (" FILENAME "): " $0
}
These are the only sensible ways to do what you want, in order of best to worst:
awk 'NR==FNR{max++; next} FNR == max { print "Final line:",$0 }' file file
awk -v max="$(wc -l < file)" 'FNR == max { print "Final line:",$0 }' file
awk 'BEGIN{ while ( (getline dummy < ARGV[1]) > 0) max++; close(ARGV[1])} FNR == max { print "Final line:",$0 }' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With