从一个文件中查找并替换另一个文件中的图像 URL

从一个文件中查找并替换另一个文件中的图像 URL

我正在尝试从文本文件中的 URL 列表替换 HTML 文件中的所有图像源 URL。

文件1.html

<td class="MetadataRes" width="380px" colspan="2" style="border-top: 1px #336699 solid;">
  <a olv_link="/Default/Scripting/ArticleWin.asp?From=Search&amp;Key=Orange/2011/03/27/129/Ad12911.xml&amp;CollName=Orange_APA3&amp;DOCID=2485870&amp;PageLabelPrint=H2&amp;Skin=%4f%72%61%6e%67%65%43%6f%75%6e%74%79%52%65%67%69%73%74%65%72&amp;AW=%31%34%31%32%36%32%38%32%31%34%35%30%32&amp;sPublication=%4f%72%61%6e%67%65&amp;sScopeID=%44%52&amp;SECTION=%43%6c%61%73%73%69%66%69%65%64&amp;sSorting=%53%63%6f%72%65%2c%64%65%73%63&amp;sQuery=%72%65%67%69%73%74%65%72%65%64%20%6e%75%72%73%65%20%3c%4f%52%3e%20%52%4e&amp;rEntityType=&amp;sSearchInAll=%66%61%6c%73%65&amp;sDateFrom=%25%33%30%25%33%35%25%32%66%25%33%30%25%33%31%25%32%66%25%33%32%25%33%30%25%33%31%25%33%30&amp;sDateTo=%25%33%30%25%33%35%25%32%66%25%33%33%25%33%31%25%32%66%25%33%32%25%33%30%25%33%31%25%33%31&amp;dc:creator=&amp;PageLabel=&amp;dc:publisher=&amp;RefineQueryView=&amp;StartFrom=%30" href="javascript:void(0);" onclick="window.top.sys.openArtWin(this.getAttribute('Olv_link'))">
    <img src="/Repository/GetImage.dll?baseHref=Orange/2011/03/27&amp;EntityID=Ad12911&amp;imgExtension=">
  </a>
</td>...

* 请在此处查看完整文件:http://pastebin.com/XbwtZJPa

文件2.txt

/getimage.dll?path=Orange/2011/03/27/129/Img/Ad1291103.gif
/getimage.dll?path=Orange/2011/03/20/133/Img/Ad1330402.gif
/getimage.dll?path=Orange/2010/08/29/137/Img/Ad1372408.gif

我想将上述 HTML 文件中图像的 URL 替换为 URL 文件中列出的第一个 URL,以获得以下内容:

结果.html

<td class="MetadataRes" width="380px" colspan="2" style="border-top: 1px #336699 solid;">
  <a olv_link="/Default/Scripting/ArticleWin.asp?From=Search&amp;Key=Orange/2011/03/27/129/Ad12911.xml&amp;CollName=Orange_APA3&amp;DOCID=2485870&amp;PageLabelPrint=H2&amp;Skin=%4f%72%61%6e%67%65%43%6f%75%6e%74%79%52%65%67%69%73%74%65%72&amp;AW=%31%34%31%32%36%32%38%32%31%34%35%30%32&amp;sPublication=%4f%72%61%6e%67%65&amp;sScopeID=%44%52&amp;SECTION=%43%6c%61%73%73%69%66%69%65%64&amp;sSorting=%53%63%6f%72%65%2c%64%65%73%63&amp;sQuery=%72%65%67%69%73%74%65%72%65%64%20%6e%75%72%73%65%20%3c%4f%52%3e%20%52%4e&amp;rEntityType=&amp;sSearchInAll=%66%61%6c%73%65&amp;sDateFrom=%25%33%30%25%33%35%25%32%66%25%33%30%25%33%31%25%32%66%25%33%32%25%33%30%25%33%31%25%33%30&amp;sDateTo=%25%33%30%25%33%35%25%32%66%25%33%33%25%33%31%25%32%66%25%33%32%25%33%30%25%33%31%25%33%31&amp;dc:creator=&amp;PageLabel=&amp;dc:publisher=&amp;RefineQueryView=&amp;StartFrom=%30" href="javascript:void(0);" onclick="window.top.sys.openArtWin(this.getAttribute('Olv_link'))">
    <img src="/Repository/getimage.dll?path=Orange/2011/03/27/129/Img/Ad1291103.gif">
  </a>
</td>...

有推荐的 shell 命令来执行此操作吗?我在运行 10.9 的 Mac 上考虑了以下 sed 命令,但遇到了错误。

$ gsed -e 's/.*SRC="\/Repository\([^"]*\)".*/\1/p{r File1.html' -e 'd}' File2.txt

答案1

假设包含EntityID一个唯一的字符串来标识 File2.txt 中的正确 URL,这不仅适用于您的示例:

sed '\_^/getimage.dll.*gif$_{H;d}
  G;s/<img src="[^"]*EntityID=\([^&]*\)&[^"]*"\(.*\)\n\(\/getimage[^\n]*\1[^.]*.gif\).*/<img src="\3"/;s/\n.*//' File2.txt File1.html

如果需要,请寻求解释。

相关内容