简体   繁体   English

如何在Sed的[]中查找文本?

[英]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.

相关问题 使用sed在匹配的子串中查找和替换 - Using sed to find and replace within matched substrings sed:如何在比赛中替换? - sed: How to substitute within a match? 使用sed查找被样式包围的文本 - Find text enclosed by patterns using sed 如何使用find,-exec,grep和sed与正则表达式来替换部分文本? - How to use find, -exec, grep, and sed with regular expression to replace part of a line of text? 如何使用sed / perl只查找2d数组并替换文本? - How to use sed/perl to find only 2d arrays and replace text? 如何使用带有regex的linux sed查找和删除文本文件中的长模式 - How to find and remove a long pattern in text files using linux sed with regex 如何在跨站点文件的值映射中查找和替换HREF链接中的6位数字,最好使用SED / Python - How to find and replace 6 digit numbers within HREF links from map of values across site files, ideally using SED/Python 如何使用perl / awk / sed搜索文件中所有用引号引起来的文本,然后将其删除? - How can I use perl/awk/sed to search for all occurrences of text wrapped in quotes within a file and then delete them? 如何在括号内查找正则表达式中的一些例外情况? - How to find text within brackets with some exceptions by regular expressions? 如何使用正则表达式在全文中查找URL - How to find a URL within full text using regular expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM