sed - 如何删除文件中以具有已知模式的行开头并以具有已知模式的行结束的多个部分

sed - how to delete multiple sections in a file that start with a line with a known pattern and end with a line with a known pattern

我有一个包含以下内容的文件 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

目的是删除所有以包含模式 'pattern 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 真的可以做到这一点吗?如果是这样,你能告诉我怎么做吗?

有了旗帜 就容易多了 awk:

start='pattern abc'
end='the_end'

awk -v f=1 -v st="$start" -v end="$end" '
match([=10=], st) {f=0}
f
match([=10=], end){f=1}' file
# prints your desired output

使用 GNU sed,这有效:

sed "/${start}/,/${end}/d" file
awk '/pattern abc/,/the_end/{next}1' file

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

这基本上是获取范围并使用 {next} 跳过它们。

最后的1只是为了强制打印。

would just delete all lines starting at the second line of the file up to and including the next to last line of the file with pattern 'the_end', leaving me with an almost empty file,

这不是发生在我身上的事情。您尝试的命令确实有效:

sed '/pattern abc/,/the_end/d' your_input_file

我正在使用 GNU sed 4.8。