使用 sed 在分隔符之间打印
这是我之前推荐的问题的扩展回答者。基本上,我需要 sed 来打印分隔符之间的文本。分隔符可以跨越多行,例如:
(abc
d)
下面是示例输入和输出。
输入
(123)
aa (a(b)cd)) (g) (c)fff
abcd(aabb
d)aa (aeb)oe
正确的输出
123
a(b
aabb
d
注意:我只想要第一对分隔符之间的文本。如果分隔符跨越两行,那么我只需要跨越两行的第一对之间的文本并移至第三行(下一行)。因此,对于输入中的最后一行,我打印了“d”并跳过(aeb)并继续到下一行。
This is an extension of my previous question recommended by the answerer. Basically, I need sed to print text between delimiters. The delimiters could span multiple lines like:
(abc
d)
Below are sample input and output.
Input
(123)
aa (a(b)cd)) (g) (c)fff
abcd(aabb
d)aa (aeb)oe
correct output
123
a(b
aabb
d
Note: I only want the text between the first pair of delimiters. If the delimiter spans two lines than I just want the text between first pair that span two lines and move on to the third (next) line. Thus, for the last line in the input I printed 'd' and skip (aeb) and move on to the next line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用了一个 sed 脚本文件(称为 sedscript ),其中包含:
和命令行:
给定输入:
它生成输出:
您可以在单个命令行上执行此操作:
第一个模式查找行包含一个左括号,后面没有一个右括号,并将下一行读入内存并重新开始。它会重复此操作,直到数据中出现右括号(或 EOF)。第二种模式在左括号之前查找内容,左括号会记住从那里到第一个右括号的数据,然后是右括号和任何其他内容;它用记住的字符串替换它并打印它。不打印任何其他内容 (
-n
)。I used a sed script file (called
sedscript
) containing:and the command line:
Given the input:
It produced the output:
You can do it on a single command line with:
The first pattern looks for lines containing an open parenthesis without a close parenthesis after it, and reads the next line into memory and starts over. It repeats that until there is a close parenthesis in the data (or EOF). The second pattern looks for the stuff before an open parenthesis, the open parenthesis, remembers the data from there to the first close parenthesis, followed by the close parenthesis and any other stuff; it replaces that with the remembered string and prints it. Anything else is not printed (
-n
).