如何在Sed中的[]中查找文本? [英] How to find text within [] in Sed?

查看:28
本文介绍了如何在Sed中的[]中查找文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于已经提出的问题.但是,我正在寻找 Sed 特定的答案.我有类似以下的文字:

This is similar to a question that has already been asked. However, I am looking for a Sed specific answer. I have text similar to the following:

一些示例文本 [带有一些额外的文本].foo

Some sample text [with some extra text].foo

我只需要抓取括号内的文本.到目前为止,我的尝试都是徒劳的.我可以使用其他工具解析该行,但似乎无法让 Sed 正确解析它.

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.

推荐答案

类似的事情?

$ 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?

这篇关于如何在Sed中的[]中查找文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆