Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 06-24-2010
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
# 2  
Old 06-24-2010
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..
# 3  
Old 06-24-2010
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
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.
# 5  
Old 06-25-2010
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..
# 6  
Old 06-25-2010
Quote:
Originally Posted by ajayakunuri
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

# 7  
Old 06-25-2010
Thanks all for your replies. It is working now
Login to Reply

« Previous Thread | Next 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
sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern Klasform Shell Programming and Scripting 9 10-12-2015 04:00 PM
Grep -v lines starting with pattern 1 and not matching pattern 2 demmel UNIX for Dummies Questions & Answers 8 07-18-2015 11:59 AM
Grep lines before a pattern having some other pattern dips_ag Shell Programming and Scripting 5 02-07-2015 01:31 PM
Grep pattern and display all lines below venkidhadha Shell Programming and Scripting 5 06-17-2014 06:59 AM
sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3 breezevinay Shell Programming and Scripting 5 06-12-2014 02:42 AM
Grep the word from pattern line and update in subsequent lines till next pattern line reached rbalaj16 Shell Programming and Scripting 5 06-18-2012 04:39 AM
Grep a pattern given in one file at other file and display its corresponding contents as output. abinash Shell Programming and Scripting 7 02-23-2011 01:51 AM
Grep pattern from different file and display if it exists in the required file abinash Shell Programming and Scripting 3 12-08-2010 08:20 AM
display content between all similar tags pattern therockravi Shell Programming and Scripting 4 10-19-2009 04:27 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


All times are GMT -4. The time now is 05:21 AM.

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