2

I am using SED and looking for printing the line matched by pattern and next 4 lines after the pattern match.

Below is the summary of my issue. "myfile.txt" content has:

As specified in doc.
risk involved in astra.
I am not a schizophrenic;and neither am I.;
Be polite to every idiot you meet.;He could be your boss tomorrow.;
I called the hospital;but the line was dead.;
Yes, I’ve lost to my computer at chess.;But it turned out to be no match for me at kickboxing.;
The urologist is about to leave his office and says:; "Ok, let's piss off now.";
What's the best place to hide a body?;Page two of Google.;
You know you’re old;when your friends start having kids on purpose.;
You won’t find anything more poisonous;than a harmonious;and friendly group of females.;
Two state clerks meet in the corridor.;One asks the other,;"Couldn't sleep either?";
Why do women put on make-up and perfume?;Because they are ugly and they smell.;
Bruce Lee’s all-time favorite drink?;Wataaaaaaaah!;
Daddy what is a transvestite?;-Ask Mommy, he knows.;
That moment when you have eye contact while eating a banana.;

I'm using below command.

sed -n -e '/You/h' -e '/Two/{x;G;p}' myfile.txt

Output by my command:

You won’t find anything more poisonous;than a harmonious;and friendly group of females.;
Two state clerks meet in the corridor.;One asks the other,;"Couldn't sleep either?";

Desired output:

You won’t find anything more poisonous;than a harmonious;and friendly group of females.;
Two state clerks meet in the corridor.;One asks the other,;"Couldn't sleep either?";
Why do women put on make-up and perfume?;Because they are ugly and they smell.;
Bruce Lee’s all-time favorite drink?;Wataaaaaaaah!;
Daddy what is a transvestite?;-Ask Mommy, he knows.;
That moment when you have eye contact while eating a banana.;
4
  • 1
    awk '/You/{ getline;x=$0} /Two/ {print x ORS $0;for(i=1;i<=4;i++) {getline ;print $0} }' inputfile , this worked for me. but people says not to use getline. so avoid and not putting it in as answer.
    – P....
    Sep 20, 2016 at 9:07
  • how about grep -A 5 "You won" myfile.txt ?
    – Sundeep
    Sep 20, 2016 at 9:17
  • I need to match two patterns 'You on first line and Two on second line respectively.' As I have many You won't statements that I don't want,
    – Pooja
    Sep 20, 2016 at 9:27
  • pluse-uno for well constructed Q! Thanks for including input/required output/current code/current output. Keep posting and good luck to all.
    – shellter
    Sep 20, 2016 at 12:30

3 Answers 3

3

With GNU sed:

sed -n '/You/h;{/Two/{x;G;};//,+4p}' myfile.txt

Output:

You won’t find anything more poisonous;than a harmonious;and friendly group of females.;                                                                                                                                                     
Two state clerks meet in the corridor.;One asks the other,;"Couldn't sleep either?";                                                                                                                                                         
Why do women put on make-up and perfume?;Because they are ugly and they smell.;                                                                                                                                                              
Bruce Lee’s all-time favorite drink?;Wataaaaaaaah!;                                                                                                                                                                                          
Daddy what is a transvestite?;-Ask Mommy, he knows.;                                                                                                                                                                                         
That moment when you have eye contact while eating a banana.;  

Explanation:

  • /You/h: copy matching line into the hold space. As there is only one hold space, h will store the last line matching You (ie You won’t...)
  • /Two/{x: when Two is found, x exchange the pattern space with the hold space. At this point:

    into pattern space: You won’t find anything more poisonous;than a harmonious;and friendly group of females.;

    into hold space: Two state clerks meet in the corridor.;One asks the other,;"Couldn't sleep either?";

  • G: appends a new line to the pattern space and copies the hold space after the new line
  • //,+4p is an address range starting from // (empty address repeats the last regular expression match, ie first 2 lines matching), up to next 4 lines +4. The address range is output with p
4
  • good one! this would work for arbitrary number of lines after match, for ex: 98 as mentioned by OP in comments..
    – Sundeep
    Sep 20, 2016 at 10:07
  • I'm trying the decipher what you have written but not able to get how it is doing it? Please help and explain.
    – Pooja
    Sep 20, 2016 at 10:13
  • Thankyou very much... @Kenavoz You rock. I understand it now.
    – Pooja
    Sep 20, 2016 at 13:48
  • Does this match You and Two in consecutive lines?
    – potong
    Sep 20, 2016 at 13:55
1

maybe this help you;

sed -n -e '/You/h' -e '/Two/{N;N;N;N;x;G;p}' myfile.txt

Example;

user@host:/tmp$ sed -n -e '/You/h' -e '/Two/{N;N;N;N;x;G;p}' myfile.txt
You won’t find anything more poisonous;than a harmonious;and friendly group of females.;
Two state clerks meet in the corridor.;One asks the other,;"Couldn't sleep either?";
Why do women put on make-up and perfume?;Because they are ugly and they smell.;
Bruce Lee’s all-time favorite drink?;Wataaaaaaaah!;
Daddy what is a transvestite?;-Ask Mommy, he knows.;
That moment when you have eye contact while eating a banana.;
2
  • 1
    Hi Muzido, Thanks. Do I have to specify N;N;N;N; for it 4 times? Isn't there any way to add 4N; kind of stuff. For another keywords I have to print 98 lines. Do I have to write 98 times N; for it?
    – Pooja
    Sep 20, 2016 at 9:13
  • Does this match You and Two in consecutive lines?
    – potong
    Sep 20, 2016 at 13:56
1

This might work for you (GNU sed):

sed -r 'N;/You.*\n.*Two/{:a;$!{N;s/\n/&/4;Ta};p;d};D' file

Read two lines into the pattern space, pattern match and then print four further lines (if possible). Otherwise, delete the first line and repeat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.