sed replace text - escape characters
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
Related videos on Youtube
George Tasioulis
Updated on September 18, 2022Comments
-
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/8G5mcxpJI 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 over 11 yearsHave you checked my Update, 50 minutes ago...?
-
George Tasioulis over 11 yearsquanta answered 19 minutes ago with a working answer based on my updated question, which worked. Thus I accepted his answer.
-
-
George Tasioulis over 11 yearsThis gives me an
sed: -e expression #1, char 3: unterminated
s' command` error. -
George Tasioulis over 11 yearsI 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 over 11 yearsIf this pattern occurs several times per line, you have to add a
g
at the end, likesed '#foo#bar#g' myFile
-
George Tasioulis over 11 yearsTried 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 over 11 yearsWell 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 over 11 yearsTried 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 over 11 yearsEscape the additional backslash with a backslash
sed 's/<a href=\\"\/tag\//<a href=\\"http:\/\/www.mydomain.com\/tag\//g' wordpress_posts.sql
. -
George Tasioulis over 11 yearsYeap now it worked :)
-
scai over 11 yearsThe backslash at
href=\"/tag/
was missing, I updated my answer. -
tacotuesday over 11 yearsPeople should really use other delimiters, as you've pointed out. "Picket fences" cause confusion and more often lead to syntax error.