Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)

share|improve this question
    
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 ! –  Iain Aug 19 '12 at 12:30
    
Have you checked my Update, 50 minutes ago...? –  George Tasioulis Aug 19 '12 at 12:33
    
It doesn't matter really - non of the answers have been updated since, they all work with your original data. –  Iain Aug 19 '12 at 12:35
    
quanta answered 19 minutes ago with a working answer based on my updated question, which worked. Thus I accepted his answer. –  George Tasioulis Aug 19 '12 at 12:41

3 Answers 3

up vote 3 down vote accepted

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
share|improve this answer
    
This gives me an sed: -e expression #1, char 3: unterminated s' command` error. –  George Tasioulis Aug 19 '12 at 8:56
    
Are you sure you specify enough 3 delimiters 's/pattern/replacement/g'? –  quanta Aug 19 '12 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. –  George Tasioulis Aug 19 '12 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. –  quanta Aug 19 '12 at 12:16
    
Yeap now it worked :) –  George Tasioulis Aug 19 '12 at 12:26

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
share|improve this answer
    
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/. –  George Tasioulis Aug 19 '12 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 –  scai Aug 19 '12 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. –  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. –  nojak Aug 19 '12 at 18:35
$ cat wordpress_posts.sql | sed 's/\/tag\//http:\/\/www.mydomain.com\/tag\//' > wordpress_posts.sql
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.