如何使用sed替换带有反斜杠然后单引号(\')的单引号(')?

时间:2011-11-09 19:58:25

标签: sed terminal

我如何使用反斜杠替换单引号(')然后使用sed替换单引号(\')?

sed s/\'/\\\'/

无效,因为你永远不会写文字。

sed ":a;N;s/\'/\\'/g" <file1 >file2

将无效,因为反斜杠将不再逃避引用,它被视为正则表达式引用。

5 个答案:

答案 0 :(得分:8)

引用替换

$ echo \' | sed s/\'/"\\\'"/
$ \'

e.g

$ cat text1
this is a string, it has quotes, that's its quality
$ sed s/\'/"\\\'"/ text1 > text2
$ cat text2
this is a string, it has quotes, that\'s its quality

答案 1 :(得分:3)

尝试以下方法:

sed -e s/\'/\\\\\'/g input > output

证明这有效:

echo "Hello 'World'" | sed -e s/\'/\\\\\'/g

输出应为:

Hello \'World\'

答案 2 :(得分:0)

怎么样: sed“s,',BBBB',g”文件 其中B是反斜杠......这是4个反斜杠......

答案 3 :(得分:0)

使用-e选项。

sed -e s / \'/ \\'/ g file2

答案 4 :(得分:-1)

这似乎有效:

<<<"''''" sed 's/'\''/\\&/;s/\('"'"'\)\(..\)$/\\\1\2/;'s/\'\'$/\\\\\'\'/";s/'$/\\\'/"
\'\'\'\'
相关问题