假设我有这样一个站点地图文件:
<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>发布于 2018-07-27 18:22:11
下面的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>https://stackoverflow.com/questions/51563323
复制相似问题