sed replace text - escape characters

14,324

Solution 1

No need to pipe cat to sed:

$ sed 's/<a href="\/tag\//<a href="http:\/\/www.mydomain.com\/tag\//g' wordpress_posts.sql

  • Remove the percentage sign
  • You only need to escape the slash
  • Specify an in-place editing (-i) if you want

Solution 2

You don't have to escape /, you can just use any other delimiter:

sed 's#<a href=\\"/tag/#<a href="http://www.mydomain.com/tag/#' wordpress_posts.sql

Solution 3

$ cat wordpress_posts.sql | sed 's/\/tag\//http:\/\/www.mydomain.com\/tag\//' > wordpress_posts.sql
Share:
14,324

Related videos on Youtube

George Tasioulis
Author by

George Tasioulis

Updated on September 18, 2022

Comments

  • George Tasioulis
    George Tasioulis over 1 year

    I have a mysqldump file in which I want to replace the following text <a href="/tag/ with <a href="http://www.mydomain.com/tag/ but can't find a way to correctly escape all special characters.

    I'm using the following command (+ a few other variants)
    cat wordpress_posts.sql | sed 's%/<a href="\/\tag\/\/<a href="\http:/\/\www.mydomain.com/\tag/\/%g' > wordpress_posts_new.sql
    but it's not working.

    Can anybody help?

    Update 1: Turns out that the source string in the mysqldump isn't <a href="/tag/ but <a href=\"/tag/ (note the extra backslash after the equal symbol)
    Here's a pastebin of one line of the SQL file which contains the string I want to replace: http://pastebin.com/8G5mcxpJ

    I tried all 3 suggested versions of the sed command, but none would replace the above string with <a href=\"http://www.mydomain.com/tag/ (yes I added the backslash after the equal symbol)

    • George Tasioulis
      George Tasioulis over 11 years
      Have you checked my Update, 50 minutes ago...?
    • George Tasioulis
      George Tasioulis over 11 years
      quanta answered 19 minutes ago with a working answer based on my updated question, which worked. Thus I accepted his answer.
  • George Tasioulis
    George Tasioulis over 11 years
    This gives me an sed: -e expression #1, char 3: unterminated s' command` error.
  • George Tasioulis
    George Tasioulis over 11 years
    I hoped that this would work but it didn't :( original and new file have the same size, plus with a grep I can still see instances of <a href="/tag/.
  • scai
    scai over 11 years
    If this pattern occurs several times per line, you have to add a g at the end, like sed '#foo#bar#g' myFile
  • George Tasioulis
    George Tasioulis over 11 years
    Tried that too, but no. I updated the post with a link to a pastebin containing one line of the sql dump which contains the string I want to replace.
  • George Tasioulis
    George Tasioulis over 11 years
    Well I simply copy-pasted your command and changed www.mydomain.com to my own domain, so I suppose it should have worked. I updated my initial post and have added a pastebin link with a line of the sql file containing the string I want to replace.
  • George Tasioulis
    George Tasioulis over 11 years
    Tried that too, but it didn't work :( I've added a link to a pastebin with the string I want to find and replace to my initial post (Update 1). Maybe this will help..
  • Greg Petersen
    Greg Petersen over 11 years
    Escape the additional backslash with a backslash sed 's/<a href=\\"\/tag\//<a href=\\"http:\/\/www.mydomain.com\/tag\//g' wordpress_posts.sql.
  • George Tasioulis
    George Tasioulis over 11 years
    Yeap now it worked :)
  • scai
    scai over 11 years
    The backslash at href=\"/tag/ was missing, I updated my answer.
  • tacotuesday
    tacotuesday over 11 years
    People should really use other delimiters, as you've pointed out. "Picket fences" cause confusion and more often lead to syntax error.