grep and display few lines before and after


 
Thread Tools Search this Thread
Operating Systems Solaris grep and display few lines before and after
# 8  
Old 07-23-2008
Code:
awk:
nawk -v ran=$1 -v pat=$2 '{
arr[NR]=$0
if (index($0,pat)!=0)
        line=NR
}
END{
for(i=line-ran;i<=line+ran;i++)
        print arr[i]
}' file

perl:
$pat=shift;
$ran=shift;
open FH,"<a";
while(<FH>){
$arr[$.]=$_;
if (index($_,$pat)>=0){
$line=$.;
}
}
close(FH);
for($i=$line-$ran;$i<=$line+$ran;$i++)
{
print $arr[$i];
}


Last edited by summer_cherry; 07-23-2008 at 03:11 AM..
# 9  
Old 09-25-2008
Quote:
Originally Posted by Ygor
Try...
Code:
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1

...where "b" and "a" are the number of lines to print before and after string "s".
Perfect!!.. This is what I was looking for. Thanks!
# 10  
Old 02-02-2009
awesome!!!
# 11  
Old 02-18-2009
nawk - header

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1

This one liner really helped me to get what I want but there is little deviant to my problem...

I want to search for a string and go back several lines(the number of lines is not fixed) and catch a pattern which is associated with the searched string.
for eg:
<HEADER>
line1
line2
line3
line4
............
<string>
I want to somehow associate <HEADER> line once I find my <string> and discard the rest of results..As I said, the number of lines is not fixed till we traverse back to <HEADER> line.

Is there any way we can achieve this?.

-Anduzzi
# 12  
Old 02-18-2009
Try...
Code:
awk '$0~s,$0~t' s="<HEADER>" t="<string>" file1

# 13  
Old 02-19-2009
Could you please explain the command and for some reason the syntax doesnt quite work for me ?.

Thx !
# 14  
Old 02-19-2009
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display lines for a particular year in a file using grep

hi, I have a log file with data for more than 3 years, i want only the rows for the year 2017, say for example. My file has the data like this 08-OCT-2015 11:17:35 AAA, BBBB 08-OCT-2017 11:17:35 AAA,Bdfdfd,dfdfd,dfd 08-Nov-2017 11:17:35 AAA,Bdfdfd,dfdfd,deree i want the rows... (2 Replies)
Discussion started by: skoshekay
2 Replies

2. Shell Programming and Scripting

Grep and display multiple lines

Hi guys, I have a log file that generates multiple logs about a query. <query time='2016-04-13 13:01:50.825'> <PagingRequestHandler> <Before>brand:vmu</Before> <After>brand:vmu</After> </PagingRequestHandler> <GroupDeviceFilterHandler> <Before>brand:vmu</Before> ... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. 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

4. Shell Programming and Scripting

Grep multiple exact match, do not display lines

Hi, Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word. CONFSUCCESS CONFFAIL CONFPARTIALSUCCESS >cat x xczxczxczc zczczcxx CONFSUCCESS czczczcczc czxxczxzxczcczc CONFFAIL xczxczcxcczczc zczczczcz CONFPARTIALSUCCESS czczxcxzc ... (4 Replies)
Discussion started by: rajeshwebspere
4 Replies

5. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

6. UNIX for Dummies Questions & Answers

Using grep and zgrep then display the next few lines

Hello everyone. I would like to know if I can use grep or zgrep to search for a particular pattern then print the x number of lines after the pattern was found. Lets say for example a pattern was found on line 3, I wanted the output to show lines 3, 4 and 5. Thanks! (10 Replies)
Discussion started by: khestoi
10 Replies

7. Shell Programming and Scripting

grep and display lines from a file

I have to grep on a few words in a file and then display the line containing those words and the line above it. For ex - File1.txt contains... abc xyz abc This is a test Test successful abc xyz abc Just a test Test successful I find the words 'Test successful' in the file... (6 Replies)
Discussion started by: user7617
6 Replies

8. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

9. UNIX for Dummies Questions & Answers

Help with "grep", need to display several lines

I have a file from which I need to collect lines to another file. I know how to use 'grep' for this, but I don't know how to do when I wan't several lines after the search word, and the amount of lines I need varies from case to case. The file I search in looks something like this: >8534734... (9 Replies)
Discussion started by: Draken
9 Replies

10. Shell Programming and Scripting

Make grep -c display like grep -n?

Hey Guys, Wondering if there is a way to do the following I have a file called test.txt abc def abc abc def I have a pattern file called pattern.txt containing the following abc def I want to do a count, but have it display the count value preceeding each line like grep -n (2 Replies)
Discussion started by: Jerrad
2 Replies
Login or Register to Ask a Question