Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace with sed on Mac OS X Leopard doesn't do what expected

I'm trying to replace \" (backslash doouble quote) by ' (quote) using sed.

sed "s/\\\"/\'/g" file.txt

The command doesn't work as expected. It replaces all the " in the text file, not only the \".

It does the same thing as sed "s/\"/\'/g" file.txt

I'm working on Mac OS X Leopard.

Does any one have a clue?

like image 677
jlfenaux Avatar asked Feb 13 '10 16:02

jlfenaux


2 Answers

You're dealing with the infamous shell quoting problem. Try using single quotes around the s//g instead, or add an extra escape:

sed "s/\\\\\"/\'/g"
like image 155
Paul Tomblin Avatar answered Oct 13 '22 10:10

Paul Tomblin


Quoting issues with bash are fun.

$ cat input
"This is an \"input file\" that has quoting issues."
$ sed -e 's/\\"/'"'"'/g' input
"This is an 'input file' that has quoting issues."

Note that there are three strings butted together to make the sed script:

  • s/\\"/
  • '
  • /g

The first and last are quoted with single quotes, and the middle is quoted with double quotes.

Matthew's command works by joining two strings instead of three:

  • s/\\"/
  • '/g

where the first is single-quoted and the second double-quoted.

like image 27
Greg Bacon Avatar answered Oct 13 '22 10:10

Greg Bacon