Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I replace a single quote (') with a backslash then single quote (\') using sed?

Tags:

terminal

sed

How would I replace a single quote (') with a backslash then single quote (\') using sed?

sed s/\'/\\\'/

won't work because you never get to write the literal .

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

won't work because the backslash will no longer escape the quote, it get's treated like a regex quote.

like image 466
Chris Hanson Avatar asked Nov 09 '11 19:11

Chris Hanson


1 Answers

just quote the replacement

$ 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
like image 92
meouw Avatar answered Oct 21 '22 20:10

meouw