Ubuntu – sed from terminal when replacing double quotes with single quotes

bashcommand linesed

I have

echo "A url: \\\"http:..."

which reads "A url: \"http:..." (<- this is how it is in my text file)

I need

echo "A url: \'http:..."

however because of all the quotes my sed command does not work properly

echo "A url: \\\"http:..." | sed -r 's/\\"+/\\\'/g'

I can see why it fails (the single quote in the middle of the sed statement is the problem, but what can I do about it? I tried looking for a third type of quotes to use, but could not find one. Any hacks?

Best Answer

If you want to change any occurrence of \" with \', you can do this:

$ echo "A url: \\\"http:..." | sed -r "s/\\\"/\\'/g"
A url: \'http:...

Just use " for the outer quotes in your sed and you can then use ' in the replacement. You don't want "+ unless you might have more than one consecutive " and you want to replace all of them. If you do, use sed -r "s/\\\"+/\\'/g".