Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
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 –  fpmurphy1 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
add comment

1 Answer

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>
share|improve this answer
    
But if HTML is malformed, this solution works? –  Sebtm Mar 7 '10 at 15:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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