簡體   English   中英

如何使用SED在文件的兩個連續行中搜索兩個不同的模式,並在模式匹配后打印下4行?

[英]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?

我正在使用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.;

使用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輸出

也許這對您有幫助;

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):

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

在模式空間中讀取兩行,進行模式匹配,然后再打印四行(如果可能)。 否則,刪除第一行並重復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM