sed替换两个双引号并保留文本 [英] sed replace two double quotes and keep text
本文介绍了sed替换两个双引号并保留文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要处理的文本:
""text""
"text"
""
需要的输出:
"text"
"text"
""
尝试过:
echo -e '""text""\n"text"\n""' | sed -e 's/"".*""/".*"/g'
但是显然没有运气.
干杯
推荐答案
您要在sed命令中使用向后引用(引用先前匹配的部分):
You want to use a backreference (something that refers to a previously matched part) in your sed command :
echo -e '""text""\n"text"\n""' | sed -E -e 's/""(.*)""/"\1"/g'
这是我所做的修改:
- 我将匹配模式中双引号内的内容与
(...)
分组在一起 - 我在替换模式中用
\ 1
引用了该匹配组 - 我告诉sed使用
-E
xtended正则表达式,这样我就不必逃避分组括号了
- I grouped what was inside the double-quote in the matching pattern with
(...)
- I referenced that matching group in the replacement pattern with
\1
- I told sed to use
-E
xtended regex so I wouldn't have to escape the grouping parenthesis
这篇关于sed替换两个双引号并保留文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文