通过grep查找并替换特殊字符前的第一个双引号

时间:2014-04-28 20:57:52

标签: regex replace sed grep xargs

我有一大堆文件,其中都有文本块,如下所示:

"Each file has different text between the opening double-quote
and the closing right-quote (or whatever it's called)”

也许不相关,但在过去我使用grep进行搜索并替换为:

grep -Rl 'search' ./path/to/files/ | xargs sed -i 's/search/replace/g

有没有办法做类似的事情,但是使用正则表达式用左引号()替换开头的普通旧双引号?替换正确的双引号字符的唯一可靠方法是搜索右引号,然后向后搜索他之前的双引号。我认为。我不确定这是否可能或如何做到。

我可以用PHP脚本来做,但是我不会从命令行看看它是否可行。

1 个答案:

答案 0 :(得分:2)

您可以使用sed:

sed -i.bak 's/"\([^”]*”\)/“\1/' file

cat file
“Each file has different text between the opening double-quote and the closing right-quote (or whatever it's called)”
相关问题