使用 sed 在分隔符之间打印文本 [英] Print text between delimiters using sed
问题描述
假设我有 op(abc)asdfasdf
并且我需要 sed
在括号之间打印 abc
.什么对我有用?(注意:我只想要一行中第一对分隔符之间的文本,如果特定的输入行没有一对括号,则不需要.)
Suppose I have op(abc)asdfasdf
and I need sed
to print abc
between the brackets. What would work for me? (Note: I only want the text between first pair of delimiters on a line, and nothing if a particular line of input does not have a pair of brackets.)
推荐答案
sed -n -e '/^[^(]*(\([^)]*\)).*/s//\1/p'
该模式查找以零个或多个非开括号字符列表开头的行,然后是开括号;然后开始记住零个或多个不是右括号的字符列表,然后是右括号,然后是任何字符.用您记住的列表替换输入并打印它.-n
表示默认不打印" - 任何没有括号的输入行都不会被打印.
The pattern looks for lines that start with a list of zero or more characters that are not open parentheses, then an open parenthesis; then start remembering a list of zero or more characters that are not close parentheses, then a close parenthesis, followed by anything. Replace the input with the list you remembered and print it. The -n
means 'do not print by default' - any lines of input without the parentheses will not be printed.
这篇关于使用 sed 在分隔符之间打印文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!