2010-11-23 35 views
3

我想改變在HTML文件中的所有鏈接替換使用的sed這樣URL使用sed

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

,但它不工作。

我的鏈接:

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

我的腳本改變只mylink.com/help/rss.php到someurl.com/help/rss.php

我需要改變只someurl.com

+0

正則表達式正如我所料。你期望/希望結果如何? – 2010-11-23 01:40:25

回答

0

您已經以\/結束它,這意味着它將轉到最後一個斜線。取出後\/,它會工作:

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

或者符合丹尼斯的有關分隔符明智的建議編輯(仍然在搜索模式,現在越來越明顯的結束卸下/):

$ 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> 
6

取出第一個斜槓後的空間,改變所有的sed斜槓的可讀性另一個字符如|並刪除所有從URL斜槓逃逸。

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