假设我有一个站点地图文件,如下所示:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.sampledomain.com/foo.html</loc>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://www.anotherdomain.nl/hello.html</loc>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
...
</urlset>
我想将所有URL(未硬编码为www.sampledomain.com和www.anotherdomain.nl(替换为www.mynewwebsite.org/,而不更改文件夹/页面路径。
使用bash可以做到这一点吗?
编辑:所需输出:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.mynewwebsite.org/foo.html</loc>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://www.mynewwebsite.org/hello.html</loc>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
...
</urlset>
以下sed命令仅在<loc>
标记内应用替换:
sed 's@<loc>.*www.w*.w*/@<loc>https://www.mynewwebsite.org/@' inputfile
在这种情况下,使用@
作为sed的分隔符是有用的,因为我们不必转义斜杠。对于您的输入文件,将生成以下输出:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.mynewwebsite.org/foo.html</loc>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://www.mynewwebsite.org/hello.html</loc>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
...
</urlset>