bash URL 替换为 sed

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4251934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:57:44  来源:igfitidea点击:

URL replace with sed

bashsed

提问by w4h

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

我想像这样使用 sed 更改 html 文件中的所有链接

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

我的脚本仅将 mylink.com/help/rss.php 更改为 someurl.com/help/rss.php

I need to change to only someurl.com

我只需要更改为 someurl.com

回答by Paused until further notice.

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

去掉第一个斜杠后面的空格,把所有的sed斜杠都改成另一个字符,例如|为了可读性,并删除所有从 URL 斜杠中转义的字符。

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

回答by Chris Morgan

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>