简体   繁体   English

重击,选择包含特定匹配项的文本

[英]Bash, selecting text surrounded by specific matches

What I need to do is print out the text which starts after the particular match and ends before another match. 我需要做的是打印出在特定比赛之后开始并在另一场比赛之前结束的文本。

Let's say I have a big output and I only need to print the text which starts after line '$$$$$$$$$$' and ends before line '((((((((('. Oh and the file might end without the '(((((((((' line, in which case I would still need that text after '$$$$$$$$$$' till the end of the output. 假设我有一个很大的输出,我只需要打印在'$$$$$$$$$$'行之后到'((((((((文件可能没有'((((((((((')line,在这种情况下,我仍然需要'$$$$$$$$$$$$'之后的文本,直到输出结束。
How would I do that? 我该怎么做?

You can use sed : 您可以使用sed

sed -e '/\$\{10\}/,/(\{9\}/!d' inputfile

To exclude the boundary lines, you have to do some programming: 要排除边界线,您必须进行一些编程:

sed -ne '/\$\{10\}/ {n;b c}; d;:c {/(\{9\}/ {d};p;n;b c}' inputfile

You can use awk 你可以使用awk

awk '/START/{f=1;next} /END/{f=0} f' file

You can set start stop to what you like. 您可以将start stop设置为您喜欢的。

cat file 猫文件

test
$$$$$$$$$$
more
data
(((((((((
stop

awk '/\$\$\$\$\$\$\$\$\$\$/{f=1;next} /\(\(\(\(\(\(\(\(\(/{f=0} f' t
more
data

Since $ and ( is special characters, you need to escape them. 由于$(是特殊字符,因此需要对其进行转义。

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

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