用 Sed Linux 替换双反斜杠双引号

Replacing Double Backslash Double Quotes with Sed Linux

我想在用 \\' 处理我的文件之前替换 \\",然后在处理文件之后我需要将 \\' 替换回 \\"。

输入文件testreplace

"dsd" "\N" "gary\""
"ghj" "fandango\\"" "\N"
"jjj"" "hjh" "gfgfgfg\"
"ee" "gg\"" "\N"

使用 sed 将 //" 替换为 //' 时出现问题。我使用了以下命令。

sed "s/\\\"/\\\'/g" testreplace > testreplaceopt

但此命令也将 \" 替换为 \' 但我想在只有两个反斜杠出现时进行替换

处理文件后,我尝试使用以下命令将 \\' 替换为 \\"

sed "s/\\\'/\\\"/g" testreplaceopt > testreplace_back

但它没有被替换

如有任何帮助,我们将不胜感激。

sed 替换中可以使用不同的分隔符。

sed语句中使用井号或加号,双引号中使用单引号。

我想提及的另一件事是使用双反斜杠字符,而不是在单引号语句中为每个反斜杠字符使用一个字符。

例如:

$ cat /tmp/aa |sed -r 's+\\"+\\'"'"'+g'
"dsd" "\N" "gary\'"
"ghj" "fandango\\'" "\N"
"jjj"" "hjh" "gfgfgfg\'
"ee" "gg\"" "\N"

或者如果您想在 sed 语句中使用“+”字符而不是“/”:

$ cat /tmp/aa |sed -r 's+\\"+\\'"'"'+g'
"dsd" "\N" "gary\'"
"ghj" "fandango\\'" "\N"
"jjj"" "hjh" "gfgfgfg\'
"ee" "gg\"" "\N"

Using different delimiters in sed

希望对你有帮助