Replacing single quotes with double quote in a file

sed

I need to replace all single quotes ' contained in /tmp/myfile with " (double quotes)

I'm using this

sed -i 's/'/\"/g' /tmp/myfile

and other combinations but I cannot find a way which works.

Any help please.

Best Answer

To replace single quotes (') it's easiest to put the sed command within double quotes and escape the double quote in the replacement:

$ cat quotes.txt 
I'm Alice
$ sed -e "s/'/\"/g"  quotes.txt 
I"m Alice

Note that the single quote is not special within double quotes, so it must not be escaped.

If, instead one wants to replace backticks (`), as the question originally mentioned, they can be used as-is within single quotes:

$ cat ticks.txt
`this is in backticks`
$ sed -e 's/`/"/g'  ticks.txt
"this is in backticks"

Within double quotes, you'd need to escape the backtick with a backslash, since otherwise it starts an old-form command substitution.

See also:

Related Question