Unix/Linux Go Back    


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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-24-2010   -   Original Discussion by ajayakunuri
ajayakunuri's Unix or Linux Image
ajayakunuri ajayakunuri is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 14 November 2017, 1:53 PM EST
Posts: 52
Thanks: 9
Thanked 2 Times in 2 Posts
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 Unix and Linux 06-24-2010   -   Original Discussion by ajayakunuri
panyam's Unix or Linux Image
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 13 April 2017, 4:34 AM EDT
Posts: 1,160
Thanks: 20
Thanked 105 Times in 100 Posts
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 11:06 AM..
Sponsored Links
    #3  
Old Unix and Linux 06-24-2010   -   Original Discussion by ajayakunuri
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 17 December 2017, 3:56 PM EST
Location: NM
Posts: 11,255
Thanks: 578
Thanked 1,122 Times in 1,034 Posts
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 Unix and Linux 06-25-2010   -   Original Discussion by ajayakunuri
ajayakunuri's Unix or Linux Image
ajayakunuri ajayakunuri is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 14 November 2017, 1:53 PM EST
Posts: 52
Thanks: 9
Thanked 2 Times in 2 Posts
Hi,
Thanks for your replies, but they dint work Linux

@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 Unix and Linux 06-25-2010   -   Original Discussion by ajayakunuri
pseudocoder's Unix or Linux Image
pseudocoder pseudocoder is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 17 February 2012, 6:42 AM EST
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts

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 04:36 AM..
Sponsored Links
    #6  
Old Unix and Linux 06-25-2010   -   Original Discussion by ajayakunuri
panyam's Unix or Linux Image
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 13 April 2017, 4:34 AM EDT
Posts: 1,160
Thanks: 20
Thanked 105 Times in 100 Posts
Quote:
Originally Posted by ajayakunuri View Post
Hi,
Thanks for your replies, but they dint work Linux

@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 Unix and Linux 06-25-2010   -   Original Discussion by ajayakunuri
ajayakunuri's Unix or Linux Image
ajayakunuri ajayakunuri is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 14 November 2017, 1:53 PM EST
Posts: 52
Thanks: 9
Thanked 2 Times in 2 Posts
Thanks all for your replies. It is working now Linux
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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



All times are GMT -4. The time now is 09:30 PM.