sed from terminal when replacing double quotes with single quotes

18,897

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".

Share:
18,897

Related videos on Youtube

Mangobae
Author by

Mangobae

Updated on September 18, 2022

Comments

  • Mangobae
    Mangobae over 1 year

    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?