Pooja
Pooja

Reputation: 213

How can I search for two different patterns in two consecutive lines in a file using SED and print next 4 lines after pattern match?

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

Upvotes: 2

Views: 1428

Answers (3)

potong
potong

Reputation: 58438

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.

Upvotes: 1

SLePort
SLePort

Reputation: 15461

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

Upvotes: 3

Mustafa DOGRU
Mustafa DOGRU

Reputation: 4112

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

Upvotes: 1

Related Questions