使用sed替换双反斜杠后跟引号(\\')?

时间:2015-06-14 05:12:54

标签: regex shell unix sed

我无法在sed中替换引用\\'后面的双反斜杠。这是我当前的命令

echo file.txt | sed "s:\\\\':\\':g"

以上命令不仅会将\\'替换为\',还会将\'替换为'

我怎样才能替换完全匹配?

输入:

'one', 'two \\'change', 'three \'unchanged'

预期:

'one', 'two \'change', 'three \'unchanged'

实际:

'one', 'two \'change', 'three 'unchanged'

2 个答案:

答案 0 :(得分:4)

$ sed "s/\\\\\\\'/\\\'/g" file
'one', 'two \'change', 'three \'unchanged'

Here is a discussion on why sed needs 3 backslashes for one

答案 1 :(得分:0)

您也可以使用:

sed "s/\\\\\'/\\\'/g"