Url is replaced with sed

I want to change all the links in an html file using sed, like this

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

but it does not work.

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 only need to change to someurl.com

+3
source share
2 answers

Take out the space after the first slash, change all the sedslashes to another character, for example |, for readability, and remove all escapes from the slash.

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

You are done with \/, that is, it will go to the last slash. Delete the final \/one 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>

, ( / , ):

$ 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

Source: https://habr.com/ru/post/1776355/


All Articles