|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | 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
|
|||
|
|||
|
Printing nth and n+1th line after a pattern match
Hi ,
I want to print the nth and n+1 lines from a file once it gets a pattern match. For eg: aaa bbb ccc ddd gh jjjj If I find a match for bbb then I need to print bbb as well as 3rd and 4th line from the match.. Please help..Is it possible to get a command using sed ![]() |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
much easier with awk: Code:
awk '/bbb/ { print ; for(n=0; n<2; n++) { getline ; print } }' inputfile |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Thanks a lot Corona688 ... But is it possible to get a command in sed ..
I tired the below one .. but looks like some issue is there with the command .. sed -n '/bbb/,+2p' filename |
|
#4
|
|||
|
|||
|
Right tool for the right job and all that. Why sed? Is this homework?
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
No not home work I am learning sed commands .. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
Code:
$ sed -n '/bbb/ {p;n;p;n;p;}' infile |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Try this one Code:
grep -A2 'bbb' inputfile |
| 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 |
| pattern match and replace another pattern in same line | anilcliff | Shell Programming and Scripting | 7 | 11-18-2011 01:45 PM |
| Print Line if next line Match a pattern | kittiwas | Shell Programming and Scripting | 2 | 10-19-2011 12:10 AM |
| Perl: Printing Multiple Lines after pattern match | Deep9000 | Shell Programming and Scripting | 5 | 07-14-2009 09:38 AM |
| Printing out pattern in line | FK_Daemon | Shell Programming and Scripting | 3 | 11-23-2007 03:27 PM |
| sed - Replace Line which contains the Pattern match with a new line | kousikan | Shell Programming and Scripting | 2 | 03-24-2007 07:24 AM |
|
|