[英]How to find text within [] in Sed?
This is similar to a question that has already been asked. 这类似于已经提出的问题。 However, I am looking for a Sed specific answer.
但是,我正在寻找针对Sed的特定答案。 I have text similar to the following:
我的文字类似于以下内容:
Some sample text [with some extra text].foo 一些示例文本[带有一些额外的文本] .foo
I need to grab just the text inside the brackets. 我只需要抓住方括号内的文本即可。 My attempts thus far have been futile.
到目前为止,我的尝试是徒劳的。 I can parse the line with other tools but I can't seem to get Sed to parse it correctly.
我可以使用其他工具来解析行,但似乎无法让Sed正确解析它。
Something like this? 像这样吗
$ echo Some sample text [with some extra text].foo | sed -e 's/.*\[\([^]]*\)\].*/\1/g'
with some extra text
$ echo Some sample text [with some extra text].foo | sed -e 's/.*\[\([^]]*\)\].*/Your text was: "\1", okay?/g'
Your text was: "with some extra text", okay?
Here is an example the replaces the text inside brackets: 这是一个替换括号内文本的示例:
$ echo 'Some sample text [with some extra text].foo' | sed -e 's/\[\(with some extra text\)\]/<\1>/g'
Some sample text <with some extra text>.foo
echo "Some [sample inside] text [with some extra text].foo" | sed -n '/\[/{ s/\]/\n/g; s/.[^\n]*\[/:/g;s/\..*//;p}'
:sample inside:with some extra text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.