如何使用SED在文件中的两个连续行中搜索两个不同的模式,并在模式匹配后打印下面的4个行?

时间:2016-09-20 08:52:32

标签: linux bash sed

我正在使用SED并寻找打印模式匹配的行以及模式匹配后的下4行。

以下是我的问题摘要。 “myfile.txt”内容包含:

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

我正在使用以下命令。

sed -n -e '/You/h' -e '/Two/{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?";

期望的输出:

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

3 个答案:

答案 0 :(得分:3)

使用GNU sed:

sed -n '/You/h;{/Two/{x;G;};//,+4p}' 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.;  

<强>解释

  • /You/h:将匹配行复制到保留空间。由于只有一个保留空间,h将存储与You匹配的最后一行(即You won’t...
  • /Two/{x:找到Two时,x会将模式空间与保留空间进行交换。此时:

    进入模式空间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?";

  • G:在模式空间中添加一个新行并在新行后复制保留空间
  • //,+4p是从//开始的地址范围(空地址重复最后一个正则表达式匹配,即前2行匹配),直到下一个4行+4。地址范围以p
  • 输出

答案 1 :(得分:1)

也许这可以帮到你;

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

实施例

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)

这可能适合你(GNU sed):

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

在图案空间中读取两行,模式匹配,然后再打印另外四行(如果可能)。否则,删除第一行并重复。