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

Linux 如何使用SED在文件中的两个连续行中搜索两个不同的图案,并在图案匹配后打印下4行?,linux,bash,sed,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

我正在使用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.;
也许这对你有帮助

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.;
使用GNU时:

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
    将存储与
    匹配的最后一行(即
    您不会…
  • /Two/{x
    :当找到
    Two
    时,
    x
    将图案空间与保留空间交换。此时:

    进入模式空间
    你不会发现比和谐友好的女性群体更有毒的东西了。

    进入等待区:
    两个州政府官员在走廊里相遇;一个问另一个,“也睡不着吗?”;

  • G
    :将新行追加到图案空间,并在新行之后复制保留空间
  • /,+4p
    是一个地址范围,从
    /
    (空地址重复最后一个正则表达式匹配,即前2行匹配),一直到下4行
    +4
    。地址范围以
    p
    输出
这可能适合您(GNU-sed):


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

awk'/You/{getline;x=$0}/two/{print x ORS$0;for(i=1;ihow about
grep-A 5“你赢了”myfile.txt
?我需要匹配两种模式“第一行的是你,第二行的是两个”。因为我有很多我不想要的You't would not语句,加上uno结构良好的Q!感谢包括输入/所需输出/当前代码/当前输出。继续发布,祝大家好运。嗨,Muzido,谢谢。我必须指定N;N;N;N吗;为了它4次?没有办法添加4N;之类的东西。对于另一个关键字,我必须打印98行。我必须写98次N;为了它吗?这个匹配你和两个连续的行吗?很好!匹配后这将适用于任意数量的行,例如:
98
,正如OP在评论中提到的那样。我正在尝试破译wh至少,你已经写了,但无法理解它是如何工作的?请帮助和解释。非常感谢…@Kenavoz you rock。我现在明白了。这是否与
你和
两个
的连续行匹配?
sed -r 'N;/You.*\n.*Two/{:a;$!{N;s/\n/&/4;Ta};p;d};D' file