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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
transforming a multiline record to single line anju Shell Programming and Scripting 9 01-14-2008 07:55 AM
appending spaces to first line based on second record. ammu Shell Programming and Scripting 2 11-16-2007 03:35 AM
Finding a character in first line of a record bsandeep_80 Shell Programming and Scripting 14 07-26-2007 11:04 AM
Showing an extra record/line srivsn Filesystems, Disks and Memory 1 05-06-2006 09:35 AM
how to extract last line in record bjorb Shell Programming and Scripting 7 10-18-2005 02:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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.
  #2 (permalink)  
Old 06-05-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
You can use the END statement to access the last record of the file:

Code:
awk  'END{ print "this is the last line :  ", $0, "  | and NR  is -> " NR, "do something else here..." }' myfile.txt
And don't use $ sign inside awk:

Code:

lastRec=`awk 'END{print NR}' myfile.txt`

awk -v aLastRec="$lastRec" '{if (NR == aLastRec) ... }' myfile.txt
  #3 (permalink)  
Old 06-05-2008
PacificWonder PacificWonder is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 10
Cool. I overlooked the $ error in awk part of the script. Your suggested solution works! Thanks a lot for your help Rubin.
  #4 (permalink)  
Old 06-05-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,368
tail can do the job here
Code:
test `tail -1 data.file`=="pattern" && echo OK || echo NOK
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0