sed replace two double quotes and keep text

2019-07-22 11:21发布

Text to be processed :

""text""
"text"
""

output desired :

"text"
"text"
""

Tried with :

 echo -e '""text""\n"text"\n""' | sed -e 's/"".*""/".*"/g'

But obviously no luck.

Cheers,

标签: bash sed replace
7条回答
再贱就再见
2楼-- · 2019-07-22 12:26

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'

Here are the modifications I did :

  • 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 -Extended regex so I wouldn't have to escape the grouping parenthesis
查看更多
登录 后发表回答