w4h
w4h

Reputation: 31

URL replace with sed

i want to change all links in html file using sed like this

s/ <a[^>]* href="[^"]*\// <a href="\http:\/\/www.someurl.com\//g

but it's not working.

My links:

<a href="http://www.mylink.com/help/rss.php" target="_top" title="RSS">RSS</a></div>

my script change only mylink.com/help/rss.php to someurl.com/help/rss.php

I need to change to only someurl.com

Upvotes: 3

Views: 5665

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360055

Take out the space after the first slash, change all the sed slashes to another character such as | for readability and remove all the escaping from the URL slashes.

sed 's|<a[^>]* href="[^"]*/|<a href="http://www.someurl.com/|g'

Upvotes: 6

Chris Morgan
Chris Morgan

Reputation: 90752

You've ended it with \/, meaning it will go to the last slash. Remove the trailing \/ and it will work:

$ echo ' <a href="http://www.mylink.com/help/rss.php" target="_top" title="RSS">RSS</a></div>' \
> | sed 's/ <a[^>]* href="[^"]*/ <a href="\http:\/\/www.someurl.com\//g'
 <a href="http://www.someurl.com/" target="_top" title="RSS">RSS</a></div>

Or, edited in line with Dennis's wise suggestion about the separator character (still with removing the / at the end of the search pattern, more obvious now):

$ echo '<a href="http://www.mylink.com/help/rss.php" target="_top" title="RSS">RSS</a></div>' \
> | sed 's|<a[^>]* href="[^"]*|<a href="http://www.someurl.com/|g'
<a href="http://www.someurl.com/" target="_top" title="RSS">RSS</a></div>

Upvotes: 0

Related Questions