Find and replace: \'

I'm trying to replace a every reference of \' with ' in a file

I've used variations of: sed -e s/\'/"\'"/g file.txt

But they always replace every.single.(single).quote

Any help would be greatly appreciated.


Not sure it's the best solution,I could do it like this:

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

(putting the backslash character in a character range so it doesn't interfere with the following quote, and protect with double quotes)

Or just extending your syntax, without quotes but using almost the same trick:

sed -e s/[\\]\'/"\'"/g file.txt

An approach trying to conserve as much of the "single-quotedness" of the sed command as possible:

sed 's/\\'"'"'/\'/g'

Just escaping \ with \\ and getting a single quote into the command with '"'"': the first single quote ends the command so far, then we have a double-quoted single quote ("'"), and finally an opening single quote for the rest of the command.

Alternatively, double quoting the whole command and escaping both the backslash and single quote:

sed "s/\\\'/\'/g"

The correct syntax is:

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

Every script (sed, awk, whatever) should always be enclosed in single quotes and you just us other single quotes to stop/restart the script delimiters break out to shell for the minimal portion of the script that's absolutely necessary, in this case long enough to use \'. You need to break out to shell to specify that ' because per shell rules no script enclosed in 's can contain a ', not even if you try to escape it.

echo "foo'bar" | gawk '{gsub(/\47/,"\\'")}1'


The tricky part here is to replace a single quote with ampersand. First in order to make the single quote manageable use its octal code here \47 and then escaping ampersand by two back slash. And all of sudden it becomes feasible :)


 ? Find and replace: \'
 ? replace with sed on Mac OS X Leopard doesn't do what expected
 ? Change find and replace every iteration
 ? Using sed to replace text with spaces with a defined variable with slashs
 ? Grep - find and replace
 ? sed command - find and replace while excluding specific pattern in find
 ? Fixed width file String replacement
 ? Use Sed to find and replace json field
 ? SSH command in Java/JSch giving exit code -1
 ? SSH command in Java/JSch giving exit code -1