0

With sed I can extract data from an HTML file? For example like this:

<html>
...
<table>
<tr>
 <td>R1A</td><td>R1B</td>
 <td>R1C</td><td>R1D</td>
</tr>
<tr>
 <td>R2X</td><td>R2Y</td>
 <td>R2W</td><td>R2Z</td>
</tr>
</table>
....
</html>

Extract this output:

R1A R1B R1C R1D
R2X R2Y R2W R2Z

In my text editor I use the following regular expression:

/<tr>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?</tr>/s
  • 3
    This is not a do my work for me site. I could solve this easily but you haven't even bothered to make a polite question out of it. – Nifle Mar 7 '10 at 14:46
  • 1
    This way madness lies: stackoverflow.com/questions/1732348/… – Dennis Williamson Mar 7 '10 at 15:00
  • Politeness helps – fpmurphy Mar 7 '10 at 15:10
  • @Nifle: I understand. I was too direct. Sorry. – Sebtm Mar 7 '10 at 15:45
  • No need to all pile onto a new user. Politeness from old users helps, too. – JRobert Mar 8 '10 at 0:10
1

Not a sed solution but an XSLT one

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" />

  <xsl:template match="//table/tr">
     <xsl:value-of select="descendant::td[1]"/>
     <xsl:text>  </xsl:text>
     <xsl:value-of select="descendant::td[2]"/>
     <xsl:text>  </xsl:text>
     <xsl:value-of select="descendant::td[3]"/>
     <xsl:text>  </xsl:text>
     <xsl:value-of select="descendant::td[4]"/>
  </xsl:template>

</xsl:stylesheet>
  • But if HTML is malformed, this solution works? – Sebtm Mar 7 '10 at 15:48

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.