read file backwards | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

read file backwards

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-27-2007
bluemoon1 bluemoon1 is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 8 February 2008, 12:27 PM EST
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
read file backwards

Assume file1 contains a list of strings.
My first script is scanning the file and deals with the lines with a certain patern in it:

grep 'somepatern' file1 \
while read LINE ; do
doSomethingAboutIt
done

Now, I need a second script that deals with the same lines (& do something differently) but in the opposite order; the last line found in the file with that patern shuld be handled first.

Please help me. Thanks so much!!!

Last edited by bluemoon1; 09-27-2007 at 11:36 PM.. Reason: typo
Sponsored Links
    #2  
Old 09-28-2007
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
You could generate a file with all the lines reversed by using something like


Code:
cat </dev/null >output
while read N
do
     echo "$N" >output.tmp
     cat output >>output.tmp
     mv output.tmp output
done

Sponsored Links
    #3  
Old 09-28-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 28 October 2014, 3:35 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
Hey, there's a new approach. For some other solutions see: sort a file in reverse order
    #4  
Old 09-28-2007
bluemoon1 bluemoon1 is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 8 February 2008, 12:27 PM EST
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks you so much, Porter & Perderabo!!
Yes, this will do (& is cute too;-)
tail -r file1
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to search a file based on a time stamp backwards 10 seconds sp3arsy Programming 2 06-14-2011 12:17 PM
Backwards question mark appearing in FTP'd file Korn0474 Shell Programming and Scripting 4 03-17-2010 01:00 PM
How to search backwards in a log file by timestamp of entries? kevinmccallum Shell Programming and Scripting 2 09-27-2009 04:07 AM
Search backwards giri_luck Shell Programming and Scripting 9 10-07-2008 02:00 AM
Scanning file backwards tekster757 UNIX for Advanced & Expert Users 2 09-21-2007 05:07 PM



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