简体   繁体   English

如何删除图案线之间的所有线

[英]How to delete all lines between the pattern lines

I have text with either the following structure 我的文字具有以下结构

bla bla more bla bla
$ 
PART  /  4402000LLINK    4401001
NAME ADHESIVE   8.0 mm LLINK Property                                           


        8.                  8.         2
END_PART
$
some other bla bla

But the line containing PART could be also: 但是包含PART的行也可能是:

PART  /  4402000   LLINK 4401001

or: 要么:

PART  /  4402000  LLINK  4401001

So strictly speaking the LLINK could occupy the columns from 16 to 23. Now I would like to delete all lines between the pattern lines. 因此严格来说,LLINK可以占据16到23列。现在,我想删除模式行之间的所有行。 First pattern is line containing both PART and this LLINK. 第一个模式是同时包含PART和此LLINK的行。 The second pattern is line containing END_PART. 第二种模式是包含END_PART的行。 So at the end I will have this: 因此,最后我将得到:

bla bla more bla bla 
$
$
some other bla bla

I am using CentOS with: 我在以下方面使用CentOS:

> echo $SHELL
/bin/tcsh

so, I can use sed or awk in tcsh eg Could you help. 因此,我可以在tcsh中使用sed或awk,例如,您能帮忙吗。 Thank you 谢谢

可以使用此sed命令。

sed -i -r '/PART.*LLINK/,/END_PART/d' file

With awk, this seems to work : 使用awk,这似乎可行:

$ cat file

bla  PART bla more bla bla
$ 
PART  /  4402000LLINK    4401001
NAME ADHESIVE   8.0 mm LLINK Property                                           
PART

        8.                  8.         2
END_PART
$
some other bla bla
AAAAA
PART  /  4402120   LLINK    4401001
NAME ADHESIVE   8.0 mm LLINK Property
PART
AAAAAAAAAA
        8.                  8.         2
END_PART
$AAAA
AAAAAsome other bla bla

awk '{if (($0!~/PART/ || $0!~/LLINK/) && stop == 0) {print} else {if ($0~/END_PART/) {stop=0} else {stop=1}}}' file

bla  PART bla more bla bla
$ 
$
some other bla bla
AAAAA
$AAAA
AAAAAsome other bla bla

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM