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
grep for a particular pattern and remove few lines above top and bottom of the patter fed.linuxgossip Shell Programming and Scripting 17 07-25-2008 08:29 AM
SED Search Pattern and Replace with the Pattern racbern Shell Programming and Scripting 4 03-15-2008 05:59 AM
Search for a pattern DNAx86 Shell Programming and Scripting 26 03-12-2008 07:03 AM
Search for a pattern from the result of search boopathi_d Shell Programming and Scripting 3 12-05-2007 09:54 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 03:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-08-2008
Prat007 Prat007 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 20
How to search for a pattern from bottom to top.

Hi,
I have a file, which is having a pattern "SEARCH" somewhere towards end of the file,

if i am giving " grep -i "SEARCH" $File" , it is taking too much time as file is very big.

So i want to search for the pattern from the back side of the file, how can we search for a pattern in bottom to top of a file.

Thanks
  #2 (permalink)  
Old 08-08-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
You can use the following commands (print the first occurence of SEARCH starting from bottom to top of file) :
Code:
tac inputfile | sed -n '/SEARCH/{p;q;}'
On my AIX boc the tac command isn't avalaible, so i use the tail command (which can't display more than 20K) :
Code:
cat -r inputfile | sed -n '/SEARCH/{p;q;}'
Code:
> ls -l bigfile
-rw-r--r-- 1 at90367 mkgroup-l-d 264904744 Aug  8 10:44 bigfile
> tail -2 bigfile
===> pattern <===
===> End Of File <===
> time grep '=> pattern <=' bigfile | tail -1
===> pattern <===

real    1m42.796s
user    0m0.249s
sys     0m0.453s
> time tac bigfile | sed -n '/=> pattern <=/{p;q;}'===> pattern <===

real    0m3.360s
user    0m0.060s
sys     0m0.030s
>
Jean-Pierre
  #3 (permalink)  
Old 08-08-2008
Prat007 Prat007 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 20
cat -r is giving me

cat: illegal option -- r
  #4 (permalink)  
Old 08-08-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
See Need to read a file in reverse

Jean-Pierre
  #5 (permalink)  
Old 08-10-2008
Prat007 Prat007 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 20
Thank you so much,
  #6 (permalink)  
Old 08-10-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
if you do not have tac or -r option from cat, you can just use tail. make a loop and keep tailing and grepping until you find the search pattern.
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 02:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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