1

I am trying to replace all the image source URLs in an HTML file from a list of URLs in a text file.

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

* See full file here: http://pastebin.com/XbwtZJPa

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

I want to replace the URL for the image in the above HTML file with the first URL listed in the URL file to get the following:

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

Is there a recommended shell command to do this? I considered the following sed command on my Mac running 10.9 but ran into errors.

$ gsed -e 's/.*SRC="\/Repository\([^"]*\)".*/\1/p{r File1.html' -e 'd}' File2.txt
  • I believe this regular expression finds all the URLs in File1.html \/Repository\/([\w\-]+(?:(?:\.[\w\-]+)+))([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])? – rbroadus Oct 15 '14 at 10:51
  • 1
    You ran into what errors? How is this different to your question here? – terdon Oct 15 '14 at 11:03
0

Assuming that the EntityID contains a unique string to identify the correct URL from File2.txt this here does the trick not only for your example:

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

Ask for the explanation if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.