|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Try sed: Code:
sed -n '/My Program Error/{N;N;p}' myfile1.txt |
| Sponsored Links | ||
|
|
|
#3
|
|||
|
|||
|
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.logIf 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
|
|||
|
|||
|
Quote:
Would you mind explaining the command a little? I can only assume N is new line. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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 | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|