更新
我假设 url 被放置到$url
变量中。然后您可以尝试以下方法:
perl -pe 'BEGIN {$url="http://somedomain.org"}
s!(\b(?:url|href)=")([^/]+)(")!$1$url/$2$3!gi' << XXX
<td><a href="testing.htm">LINK</a></td>
<td><a href="http://mydomain.com/testing.htm">LINK</a></td>
<meta http-equiv="Refresh" content="0;URL="home">
XXX
输出:
<td><a href="http://mymain.org/testing.htm">LINK</a></td>
<td><a href="http://mydomain.com/testing.htm">LINK</a></td>
<meta http-equiv="Refresh" content="0;URL="http://mymain.org/home">
它用或如果不包含字符替换所有href="..."
或url="..."
(不区分大小写)模式。href="$url/..."
url="$url/..."
...
/
如果输入的是文件,则可以直接替换文件中的这些模式:
cat >tfile << XXX
<td><a href="testing.htm">LINK</a></td>
<td><a href="http://mydomain.com/testing.htm">LINK</a></td>
<meta http-equiv="Refresh" content="0;URL="home">
XXX
cat tfile
perl -i -pe 'BEGIN {$url="http://mymain.org"}
s!(\b(?:url|href)=")([^/]+)(")!$1$url/$2$3!gi' tfile
echo "---"
cat tfile
输出:
<td><a href="testing.htm">LINK</a></td>
<td><a href="http://mydomain.com/testing.htm">LINK</a></td>
<meta http-equiv="Refresh" content="0;URL="home">
---
<td><a href="http://mymain.org/testing.htm">LINK</a></td>
<td><a href="http://mydomain.com/testing.htm">LINK</a></td>
<meta http-equiv="Refresh" content="0;URL="http://mymain.org/home">