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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to grep for a pattern and display the content above that pattern
# 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 11: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 Smilie

@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 04:36 AM..
# 6  
Old 06-25-2010
Quote:
Originally Posted by ajayakunuri
Hi,
Thanks for your replies, but they dint work Smilie

@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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

Grep lines before a pattern having some other pattern

Hi All, I am trying to fetch lines before a pattern, I got to know about -B flag in grep but we have to pass the number to get those lines before some pattern say (X), now what if I want to get line/s with some other pattern say (Y) before X pattern? How to get about it? please help. Input:... (5 Replies)
Discussion started by: dips_ag
5 Replies

5. Shell Programming and Scripting

Grep pattern and display all lines below

Hi I need to grep for a patter and display all lines below the pattern. For ex: say my file has the below lines file1 file2 file3 file4 file5 I NEED to grep for patter file3 and display all lines below the pattern. do we have an option to get this data. Let me know if you require... (5 Replies)
Discussion started by: venkidhadha
5 Replies

6. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

7. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

8. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

9. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

10. Shell Programming and Scripting

display content between all similar tags pattern

hi, I m stuck at a point for more than 3days. My doubt is pretty simple.. I have a web page content in $content. ( got this by using LWP).. Now I want to display the content matching a pattern. I tried if($content =~ m{<div class="abc">(.*?)</div>}s){ print $1;} that will... (4 Replies)
Discussion started by: therockravi
4 Replies
Login or Register to Ask a Question