[英]Grep a block of text using pattern
I have short text file, where i should make output of data using special pattern.我有短文本文件,我应该在其中使用特殊模式输出数据。 My file:
我的文件:
99 test1
88 test2
10 test3
11 test1
12 test1
13 test2
14 test3
17 test1
18 test4
One by one, from test1 to test2 and to test3.一一,从test1到test2再到test3。 So... I have written the comand:
所以......我写了命令:
sed '/test1.*\|test2.*\|test3/!d' filename
And in output i have result:在输出中我有结果:
99 test1
88 test2
10 test3
11 test1
12 test1
13 test2
14 test3
17 test1
In this case, i have lines, that i not need:在这种情况下,我有一些不需要的行:
11 test1
17 test1
This lines don't go one after one.这条线不会一条接一条。 How i can do result, that i need ?
我怎样才能得到我需要的结果? Please help me.
请帮我。 Thanks for your attention.
感谢您的关注。 i Should get this result:
我应该得到这个结果:
99 test1
88 test2
10 test3
12 test1
13 test2
14 test3
A simple quick way, although not very elegant, is to put it in one line, break it in the desired pattern, and then filter it again with the pattern:一种简单的快速方法,虽然不是很优雅,但将其放在一行中,将其拆分为所需的模式,然后再次使用该模式进行过滤:
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.