Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-02-2012
Registered User
 

Join Date: Nov 2007
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
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, but I am not getting the first line displayed.

Here is myfile1.txt


**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 123 terminated
**ID PIN 12345
Comamnd Successful
Command Terminated
Command Successful
Command Terminated
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 345 terminated
**ID PIN 67890
Comamnd Successful
Command Terminated
Command Successful
Command Terminated
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 678 terminated
**ID PIN 13579
Comamnd Successful
Command Terminated
Command Successful
Command Terminated


After I ran awk 'c-->3;/Error/{c=5}' myfile1.txt > myfile2.txt i get this

**Port 123 terminated
**ID PIN 12345
**Port 345 terminated
**ID PIN 67890
**Port 678 terminated
**ID PIN 13579

I want these lines to be displayed, but I want the line with the error in it, too.

so myfile2.txt should look like this:


**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 123 terminated
**ID PIN 12345
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 345 terminated
**ID PIN 67890
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 678 terminated
**ID PIN 13579

What am I doing wrong?

Thank you
Sponsored Links
    #2  
Old 02-02-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,021
Thanks: 9
Thanked 285 Times in 277 Posts
Try sed:

Code:
sed -n '/My Program Error/{N;N;p}' myfile1.txt

Sponsored Links
    #3  
Old 02-03-2012
Registered User
 

Join Date: Mar 2011
Posts: 547
Thanks: 35
Thanked 135 Times in 131 Posts
Please use code tags when posting i/output or code.
Your awk command is almost correct, you were really close, just add print:

Code:
awk 'c-->3;/Error/{c=5; print}'

Or, with getline and no variable:

Code:
awk '/Error/{print; getline; print; getline; print}'

Or, with getline reading into a variable instead of $0:

Code:
awk '/Error/{getline a; getline b; print $0,a,b }' OFS="\n" err.log

If the match is on the very end of file, solution 2 will print duplicates, and solution 3 will print empty lines. To treat this, you could check for the return value of getline.

Last edited by mirni; 02-03-2012 at 04:26 AM.. Reason: eof treatment
    #4  
Old 02-03-2012
Registered User
 

Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by balajesuri View Post
Try sed:

Code:
sed -n '/My Program Error/{N;N;p}' myfile1.txt

Cool! Tried it and this one works as what he wanted.
Would you mind explaining the command a little?
I can only assume N is new line.
Sponsored Links
    #5  
Old 02-03-2012
Registered User
 

Join Date: Nov 2007
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
The codes 1 and 2 are what I was looking for. Is there a way to add a blank line after the third line.

---------- Post updated at 06:16 PM ---------- Previous update was at 03:12 PM ----------

Never mind, I was able to figure it out.
awk '/Error/{print; getline; print; getline; print; print ""}' did the trick insertinbg ablank line.
Sponsored Links
Reply

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
kill a process if grep match is found burek Shell Programming and Scripting 4 11-16-2011 03:41 PM
Perl script to look for a string in a file and append few lines if the match is found arunkumarmc Programming 1 03-04-2011 05:05 PM
exact string match ; search and print match bash_in_my_head Shell Programming and Scripting 8 05-22-2010 11:41 PM
Grep and display n lines after the match is found. cv_pan UNIX for Dummies Questions & Answers 3 09-25-2008 11:15 PM
if match found go to a particular line in perl user_prady Shell Programming and Scripting 17 03-31-2008 03:00 PM



All times are GMT -4. The time now is 03:35 AM.