繁体   English   中英

使用模式 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\1\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 .

暂无
暂无

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

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