Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Want to grep for a pattern and display the content above that pattern

👤 Login to reply

    #1  
Old 06-24-2010
ajayakunuri ajayakunuri is offline
Registered User
 
Want to grep for a pattern and display the content above that pattern

Hi,
When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part.
For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep for the FAIL word and shud be able to see the content above that, only 50-60 lines.

xxxxxx
xxxxxx
xxxxxx
ODS FAILED
xxxxxx
xxxxx
xxxxx

I want the lines above that ODS FAILED.

Pls help me with this.

Thanks
Ajay
Sponsored Links
    #2  
Old 06-24-2010
panyam panyam is offline Forum Advisor  
Registered User
 
Something like this:

Code:
 
awk 'c&&c--;/FAILED/{c=50;print p;exit}{p=p"\n"$0}' log_file

Forget to say : this is suggested by one of this forum member some time back ( few months).

Last edited by panyam; 06-24-2010 at 10:06 AM..
Sponsored Links
    #3  
Old 06-24-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
with GNU grep (Linux)
Code:
grep -B 60 'ODS FAILED'  logfile

For everywhere else here is one way:
Code:
grep -n 'ODS FAILED' logfile | awk -F":" '{print $1; exit}' | read ln
start=$(( $ln - 50 ))
[[ $start -le 0 ]]  && start=1
sed -n "$start,$ln,p" logfile

    #4  
Old 06-25-2010
ajayakunuri ajayakunuri is offline
Registered User
 
Hi,
Thanks for your replies, but they dint work

@panyam: It is displaying all the lines before that. i did a wc -l and here is the output
[cmg-iqdps2p0] x772525 /usr/prod/bdw/log> awk 'c&&c--;/FAILED/{c=50;print p;exit}{p=p"\n"$0}' dm_distwatch10r.20100624.log|wc -l
621

@jim mcnamara: It is throwing an error
ksh: start=-50[[: This is not an identifier.
Sponsored Links
    #5  
Old 06-25-2010
pseudocoder's Unix or Linux Image
pseudocoder pseudocoder is offline
Registered User
 
Code:
$ perl -nle 'if (!/FAIL/) { push @x, $_; }
             else { push @x, $_; for ( $#x-50 .. $#x ) { print $x[$_] } @x=(); }' infile

Edit: This ^^ will misbehave if there are less than 50 lines before the pattern FAIL, here's the fix:
Code:
$ perl -nle 'if (!/FAIL/) { push @x, $_; }
else { push @x, $_;
if ($#x > 50) {
for ( $#x-50 .. $#x ) { print $x[$_] } @x=(); }
else { for ( 0 .. $#x ) { print $x[$_] }} @x=(); }' infile


Last edited by pseudocoder; 06-25-2010 at 03:36 AM..
Sponsored Links
    #6  
Old 06-25-2010
panyam panyam is offline Forum Advisor  
Registered User
 
Quote:
Originally Posted by ajayakunuri View Post
Hi,
Thanks for your replies, but they dint work

@panyam: It is displaying all the lines before that. i did a wc -l and here is the output
[cmg-iqdps2p0] x772525 /usr/prod/bdw/log> awk 'c&&c--;/FAILED/{c=50;print p;exit}{p=p"\n"$0}' dm_distwatch10r.20100624.log|wc -l
621
Sorry , misread it.

Something like this:

Code:
  
awk '/FAILED/{print p;exit} {p=p"\n"$0}' input_file | tail -50

Sponsored Links
    #7  
Old 06-25-2010
ajayakunuri ajayakunuri is offline
Registered User
 
Thanks all for your replies. It is working now
Sponsored Links
👤 Login to reply

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
display content between all similar tags pattern therockravi Shell Programming and Scripting 4 10-19-2009 04:27 AM
Display few rows before and after the pattern is found lavinia_f UNIX for Dummies Questions & Answers 3 05-13-2009 08:43 AM
search a pattern and if pattern found insert new pattern at the begining pitagi Shell Programming and Scripting 7 02-12-2009 09:27 PM
Replacing a paragraph between pattern , with the content 4m another file go4desperado Shell Programming and Scripting 4 12-07-2006 08:44 AM
how to display only the pattern yerics UNIX for Advanced & Expert Users 5 01-05-2004 10:07 AM



All times are GMT -4. The time now is 01:43 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?