Linux – sed replace text – escape characters

escapinglinuxreplacesearch-and-replacesed

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)

Best Answer

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
Related Topic