【问题标题】:Replace all domain names in url替换url中的所有域名
【发布时间】:2019-01-04 21:29:15
【问题描述】:

假设我有一个这样的站点地图文件:

<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.comwww.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>

【问题讨论】:

  • 你能举个例子吗?

标签: bash sed replace


【解决方案1】:

以下 sed 命令仅在 &lt;loc&gt; 标签内应用替换:

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>

【讨论】:

  • 感谢您的回答,但这不是我想要的(也不是被问到的)。我已经用所需的输出更新了我的问题。
  • 我正在尝试,但它没有提供所需的输出。
  • 这不会产生预期的输出,也不会尝试解决问题的意图(只需更改任何“.html”文件的域名并保留“.html”文件名照原样遵循),但它是公认的答案并且有多个赞成票。去图...
  • 我也对它进行了测试,它不仅没有产生预期的输出,而且根本无法产生预期的输出,因为它将用https://www.mynewwebsite.org/foo.html替换每个URL,包括https://www.anotherdomain.nl/hello.html(注意从“hello.html”更改为“foo.html”)。再次测试它,这次运行“差异”,而不是仅仅目视检查预期输出与实际输出。
  • @xDantehh 请查看更正后的命令。根据 Ed Morton 的评论,作为答案接受的命令不正确,所以我更正了它,现在它给出了所需的输出。
猜你喜欢
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 2013-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多