How to replace single quote (') with backslash and then single quote (\) with sed?

How to replace single quote (') with backslash and then single quote (\) with sed?

sed s/\'/\\\'/ 

will not work because you can never write a literal.

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

will not work because the backslash will no longer remove the quote; it is treated as a quote from the regular expression.

+6
source share
5 answers

just specify a replacement

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

eg

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

Try the following:

 sed -es/\'/\\\\\'/g input > output 

To prove that this works:

 echo "Hello 'World'" | sed -es/\'/\\\\\'/g 

The output should be:

 Hello \'World\' 
+2
source

how about: sed "s, ', BBBB', g" file where B is the backslash ... these are 4 backslashes ...

0
source

Use the -e option.

sed -es / \ '/ \\' / g file2

0
source

It works:

 <<<"''''" sed 's/'\''/\\&/;s/\('"'"'\)\(..\)$/\\\1\2/;'s/\'\'$/\\\\\'\'/";s/'$/\\\'/" \'\'\'\' 
-1
source

Source: https://habr.com/ru/post/901149/


All Articles