The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 06-05-2008
PacificWonder PacificWonder is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 10
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.