|
AWK - if last line/record do something
Hello:
I am trying to perform a certain action if the current record is the last line of the input file. But I am unable to figure out how to determine the last line of a file in awk.
I need to do something like this:
awk '{ if (lastline == NR) Do Something}' myfile.txt
I have tried the following in a Korn Shell script.
lastRec=`wc -l myfile.txt | awk '{print $1}'`
print $lastRec
awk -v aLastRec="$lastRec" '{if (NR == $aLastRec) print NR ": This is the last record!"}'
But I could not get it to recognize the $lastRec or $aLastRec.
Can you please help?
Thanks.
|