首页 > 解决方案 > sed - 如何删除文件中以具有已知模式的行开始并以具有已知模式的行结束的多个部分

问题描述

我有一个包含以下内容的文件 testing123.txt:

01 this is the start of the file
02 start of section one with pattern abc
03 first line
04 second line
05 third line
06 fourth line
07 the_end
08 start of section two with pattern xyz
09 first line
10 second line
11 third line
12 the_end
13 start of section three with pattern abc
14 first line
15 second line
16 third line
17 fourth line
18 fifth line
19 the_end
20 start of section four with pattern klm
21 first line
22 second line
23 third line
24 fouth line
25 fifth line
26 sixth line
27 the_end
28 start of section five with pattern abc
29 first line
30 3second line
31 third line
32 fourth line
33 fifth line
34 sixt line
35 seventh line
36 eighth line
37 the_end
38 start of section six with pattern tuv
39 first line
40 second line
41 the_end
42 start of section seven with pattern abc
43 first line
44 second line
45 the_end
46 start of section eight with pattern abc
47 first line
48 second line
49 third line
50 fourth line
51 the_end
52 this is the end of the file

目的是删除所有以包含模式“模式 abc”的行开头的所有部分,包括模式“the_end”的下一行。部分可以长达 14 行或短至 3 行或介于两者之间的任意数量的行。文件 testing123.txt 可以长达 1400 行。此示例中的最终结果应为:

01 this is the start of the file
08 start of section two with pattern xyz
09 first line
10 second line
11 third line
12 the_end
20 start of section four with pattern klm
21 first line
22 second line
23 third line
24 fouth line
25 fifth line
26 sixth line
27 the_end
38 start of section six with pattern tuv
39 first line
40 second line
41 the_end
52 this is the end of the file

这就是我现在所拥有的(尽管我尝试了更多):

#!/bash/bin
PATTERN_1='pattern abc'
ENDLINE='the_end'
sed -i "/$PATTERN_1/,/$ENDLINE/d" testing123.txt

然而,在这个例子中,这只会删除从文件第二行开始的所有行,直到并包括文件的最后一行,模式为“the_end”,给我留下一个几乎是空的文件,这显然不是什么我想。

这是针对我使用 GNU/sed 在 Linux (Mint) 中编写的 bash 脚本。sed 真的可以这样做吗?如果是这样,你能告诉我怎么做吗?

标签: linuxstringsedlinux-mint

解决方案


awk '/pattern abc/,/the_end/{next}1' file

编辑:意识到这是一个 sed 问题,但可能仍然有用

这基本上得到了范围并使用 {next} 跳过它们。

最后的 1 只是强制打印。


推荐阅读