Grep and display n lines after the match is found.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep and display n lines after the match is found.
# 1  
Old 09-25-2008
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!


#Method
GetPresentCode Behaviours
#Return Type
string
#Method Body
return pPresent.code
#User Tags
_DI_LABEL = editonly "--_Transaction code?"
_DI_DESCRIPTION = editonly ""
_DI_TYPE = editonly "Condition"

output as:
GetPresentCode Behaviours
#Return Type
string
#Method Body
return pPresent.code
# 2  
Old 09-25-2008
Quote:
Originally Posted by cv_pan
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!


#Method
GetPresentCode Behaviours
#Return Type
string
#Method Body
return pPresent.code
#User Tags
_DI_LABEL = editonly "--_Transaction code?"
_DI_DESCRIPTION = editonly ""
_DI_TYPE = editonly "Condition"

output as:
GetPresentCode Behaviours
#Return Type
string
#Method Body
return pPresent.code
With awk:

Code:
awk '/GetPresentCode/{c=5;next}c{c--;print}' file


Last edited by Franklin52; 09-25-2008 at 03:47 PM.. Reason: adding first opening brace, thanks era ;-)
# 3  
Old 09-25-2008
And of course grep -A 5 if your grep has that option. (You should take out the next from Franklin's solution if you want the matching line to print as well. That's what grep -A does and that's what you seem to be asking for in your example. Oh, and the opening brace before c=5 is missing.)

Last edited by era; 09-25-2008 at 03:41 PM.. Reason: Suggest removing "next" from Franklin52's solution
# 4  
Old 09-26-2008
or simply write..
Code:
awk 'c-->0;/pattern/{c=5}' filename

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep everything between two pattern if match not found

I need to help to work this Print everything between 2 patterns if grep is not found the search word example Detroit orange cat bat rat apple sed -n "/Detroit,/apple/p" d |grep chicago output would be Detroit orange cat bat rat (1 Reply)
Discussion started by: jhonnyrip
1 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

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

4. UNIX for Dummies Questions & Answers

Display lines after match is found within specified range

I have a file which has collection of segments occuring n(For eg.100) times ISA GS ST NM1*85 N3 N4 NM1*IL N3 N4 REF*D9*1001 ISE GE SE ISA GS ST NM1*85 N3 (13 Replies)
Discussion started by: nsuresh316
13 Replies

5. UNIX for Dummies Questions & Answers

Display n lines after match found and other line

I have a file like this DoctorName Address1 Address2 DOB InsuredName Address1 Address2 DOB PatientName Address1 Address2 DOB ClaimNo1 DoctorName Address1 Address2 DOB InsuredName (2 Replies)
Discussion started by: nsuresh316
2 Replies

6. UNIX for Dummies Questions & Answers

Display n lines before match found

I have a file with following data A B C D E F G H I K L M N and search pattern is G Expected output (3 Replies)
Discussion started by: nsuresh316
3 Replies

7. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

8. Shell Programming and Scripting

kill a process if grep match is found

Hi, I need something unusual, I guess. I need to start a process, and if that process displays a specific error message, I need to kill that process and restart it. Something like: startprocess | grep -i "This is the specific error message" && kill $pidof(startprocess) Explanation, I need... (4 Replies)
Discussion started by: burek
4 Replies

9. Shell Programming and Scripting

How to grep for message and if found display filename?

Hi i'm new to the forum and was hoping someone could help me with the following query. I do alot of testing and have hundreds of log files output. I have a script (someone else wrote) which finds all the passed and failed logs and puts a number in a column onto a webpage: e.g: Pass ... (4 Replies)
Discussion started by: defamer
4 Replies

10. UNIX for Advanced & Expert Users

How to prevent grep command from throwing a system trap if No match is found.

Hi How to prevent grep command from throwing a system trap(or returning error status) if No match is found in the specified file(s) ? Consider this simple shell script: #!/usr/bin/ksh trap 'STATUS=$?;set +x;echo;echo error $STATUS at line nb $LINENO executing :\ `sed -n... (2 Replies)
Discussion started by: cool.aquarian
2 Replies
Login or Register to Ask a Question