Register
It is currently Sat Oct 25, 2014 8:56 pm

[awk] Performing separate actions over different files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Aug 16, 2006 8:31 am   

Joined: Wed Aug 16, 2006 7:45 am
Posts: 1
Location: Melbourne, Australia
A lot of awk's advanced usefulness sparks from the use of builtin variables. One such variable is FNR, which is the record number of the file currently being processed.

So, if FNR is equal to 1, you are processing record number 1 (which will be line number 1 unless you modify the builtin RS variable) of a file. Here's an example:

Code:
#!/bin/awk -f

FNR == 1 {
  file++
  print "Now processing: " FILENAME
}
file == 1 { print "1) " $0 }
file == 2 { print "2) " $0 }


I use this trick when I need to process a file based on the data retrieved from the preceding file.

I'm not sure of the portability of the FNR variable across all awk implementations, but it works for gawk and mawk.


Top
 Profile WWW  
 PostPosted: Wed Aug 16, 2006 10:42 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Very cool :)

Thanks :) Welcome to bashscripts... :)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP