-1

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
  • 1,969
  • 2
  • 16
  • 17

3 Answers3

6

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
scai
  • 2,199
  • 1
  • 12
  • 16
  • 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 ` – George Tasioulis Aug 19 '12 at 08:58
  • 1
    If this pattern occurs several times per line, you have to add a `g` at the end, like `sed '#foo#bar#g' myFile` – scai Aug 19 '12 at 09:22
  • 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 Aug 19 '12 at 11:43
  • The backslash at `href=\"/tag/` was missing, I updated my answer. – scai Aug 19 '12 at 13:31
  • 1
    People should really use other delimiters, as you've pointed out. "Picket fences" cause confusion and more often lead to syntax error. – tacotuesday Aug 19 '12 at 18:35
4

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
quanta
  • 50,327
  • 19
  • 152
  • 213
3
$ cat wordpress_posts.sql | sed 's/\/tag\//http:\/\/www.mydomain.com\/tag\//' > wordpress_posts.sql
Chida
  • 2,471
  • 1
  • 16
  • 29
  • 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.. – George Tasioulis Aug 19 '12 at 11:45