[英]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. 我正在使用SED并寻找打印图案匹配的行以及图案匹配后的下4行。
Below is the summary of my issue. 以下是我的问题的摘要。 "myfile.txt" content has: “ 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.;
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.;
With GNU sed: 使用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. /You/h
:将匹配的行复制到保留空间。 As there is only one hold space, h
will store the last line matching You
(ie You won't...
) 因为只有一个保留空间,所以h
将存储与You
匹配的最后一行(即, You won't...
) /Two/{x
: when Two
is found, x
exchange the pattern space with the hold space. /Two/{x
:当找到Two
时, x
将模式空间与保留空间交换。 At this point: 这一点:
into pattern space : You won't find anything more poisonous;than a harmonious;and friendly group of females.;
进入模式空间 : 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?";
进入容纳空间 : 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 G
:在模式空间上添加新行,并在新行之后复制保留空间 //,+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
. //,+4p
是一个地址范围,从//
(空地址重复最后一个正则表达式匹配,即前2行匹配),直至下4行+4
。 The address range is output with p
地址范围用p
输出 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.;
This might work for you (GNU sed): 这可能对您有用(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. 否则,删除第一行并重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.