开发者

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


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'


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜