使用模式 Grep 一段文本

Grep a block of text using pattern

我有短文本文件,我应该在其中使用特殊模式输出数据。我的档案:

99 test1
88 test2
10 test3
11 test1
12 test1
13 test2
14 test3
17 test1
18 test4

一个接一个,从test1到test2再到test3。所以...我写了命令:

sed '/test1.*\|test2.*\|test3/!d' filename

在输出中我有结果:

99 test1
88 test2
10 test3
11 test1
12 test1
13 test2
14 test3
17 test1

在这种情况下,我有不需要的行:

11 test1

17 test1

这一行不会一字一句地讲下去。我怎样才能得到我需要的结果?请帮我。感谢您的关注。我应该得到这个结果:

99 test1
88 test2
10 test3
12 test1
13 test2
14 test3

一种简单快速的方法,虽然不是很优雅,但是将它放在一行中,将其分解为所需的模式,然后再次使用模式过滤它:

sed ':a;N;$!ba;s/\n/\n/g' < filename | \
sed 's/\([0-9][0-9] test1\n[0-9][0-9] test2\n[0-9][0-9] test3\n\)/\n\n/g' | \
egrep "[0-9][0-9] test1\\n[0-9][0-9] test2\\n[0-9][0-9] test3\\n" | \
sed 's/\n/\n/g' | grep .