0

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)

4
  • All of the answers below work with the data as specified in your question. In your real data there is an additional `\` that needs to be dealt with. GIGO ! Commented Aug 19, 2012 at 12:30
  • Have you checked my Update, 50 minutes ago...? Commented Aug 19, 2012 at 12:33
  • It doesn't matter really - non of the answers have been updated since, they all work with your original data. Commented Aug 19, 2012 at 12:35
  • quanta answered 19 minutes ago with a working answer based on my updated question, which worked. Thus I accepted his answer. Commented Aug 19, 2012 at 12:41

3 Answers 3

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
6
  • 1
    This gives me an sed: -e expression #1, char 3: unterminated s' command` error. Commented Aug 19, 2012 at 8:56
  • Are you sure you specify enough 3 delimiters 's/pattern/replacement/g'? Commented Aug 19, 2012 at 9:32
  • 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. Commented Aug 19, 2012 at 11:45
  • Escape the additional backslash with a backslash sed 's/<a href=\\"\/tag\//<a href=\\"http:\/\/www.mydomain.com\/tag\//g' wordpress_posts.sql. Commented Aug 19, 2012 at 12:16
  • Yeap now it worked :) Commented Aug 19, 2012 at 12:26
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 
5
  • 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/. Commented Aug 19, 2012 at 8: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 Commented Aug 19, 2012 at 9: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. Commented Aug 19, 2012 at 11:43
  • The backslash at href=\"/tag/ was missing, I updated my answer. Commented Aug 19, 2012 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. Commented Aug 19, 2012 at 18:35
3
$ cat wordpress_posts.sql | sed 's/\/tag\//http:\/\/www.mydomain.com\/tag\//' > wordpress_posts.sql 
1
  • 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.. Commented Aug 19, 2012 at 11:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.