0

i want to extract the value from html tag using sed

file:

<td class=xl7128074></td>
  <td class=xl7128074></td>
  <td class=xl7128074></td>
 </tr>
 <tr height=21 style='height:15.75pt'>
  <td height=21 class=xl8428074 style='height:15.75pt;border-top:none'>ANT</td>
  <td class=xl10028074 style='border-top:none;border-left:none'>$interoprfbcheck_prodconnectorstatus_ANT</td>
  <td class=xl8228074>ANT</td>

  <!-- This is set by Python -->
  <td class=xl155577 style='border-left:none;font-size:11.0pt;color:#006100;          
  font-weight:400;text-decoration:none;text-underline-style:none;text-line-through:       
  none;font-family:Arial;border-top:none;border-right:1.0pt solid windowtext;             
  border-bottom:1.0pt solid windowtext;border-left:1.0pt solid windowtext;
  background:#E6EFCE;mso-pattern:black none'>
GREEN</td>
  <td class=xl7128074></td>
  <td class=xl7128074></td>
  <td class=xl7128074></td>

I want the expected result:

ANT
$interoprfbcheck_prodconnectorstatus_ANT
ANT
GREEN

I was using sed -n 's/<td.*>\(.*\)<\/td>/\1/p' filename.html and getting below result:

ANT
$interoprfbcheck_prodconnectorstatus_ANT
ANT

the GEEEN is not coming, I think because it is not in same line, is there any thing we can add to get the result of next line as well?

  • 6
    If you wish to preserve your sanity, you should not attempt to process XML with line-oriented tools like sed. Try something like xmlstarlet or xsltproc. – Michael Vehrs Jun 6 '16 at 14:28
  • this is reflecting the desired result sed -n 's/.*>(.*)<\/td>/\1/p' filename.html. can anyone pls let me know if it is the correct command to finding pattern ending with '</td> – user173787 Jun 6 '16 at 14:31
  • 1
    Yes, it is. But sed will look at one line at a time, and the closing tag is not on the same line, so the pattern as a whole will not match. – Michael Vehrs Jun 6 '16 at 14:54
  • 1
    Don't parse XML or HTML with regular expressions. It doesn't work – cas Jun 7 '16 at 3:05
  • tr -d '\n' <filename.html | sed 's/<\/td>/<\/td>\n/g' | sed -n 's/.*<td[^>]*>\([^<]\+\)<.*/\1/p' ... but can obviously easily break. (E.g. some comment, malformed html, td tag holding another tag etc.) – Runium Jun 13 '16 at 23:34
1

You could use html2 as a "preprocessor" for sed:

$ html2 <file 2>/dev/null | sed '/td=/!d;s/^.*td=//'
ANT
$interoprfbcheck_prodconnectorstatus_ANT
ANT
 GREEN
  • +1. html2 is a fairly obscure tool so it's worth pointing out that it's in the xml2 package. or, if not packaged for your OS, the source is available at ofb.net/~egnor/xml2 – cas Jun 8 '16 at 3:42
0

GREEN is not coming as it is placed by the generator on a separate line, so your sed script does not find a match. Without special arrangements sed pulls the incoming stream into the match space line by line.

So, you need to pull into match space new lines with N command until you get closing </tr> in there, then store a copy of pattern space in a hold space, remove leading <tr ...> tag and remove everything starting from </tr> up to the end of match space and print it.

After this you need to replace the match space with content of the hold space, remove the already printed part and start over again with looking for leading <tr ...> tag.

see info sed for details

0

The HTML sample you posted is not valid XML for several reasons. If that were fixed, however, you could use this:

xmlstarlet sel -t -v '//td[text()]' input.html

The result is not exactly what you expect, since the text of the last non-empty td is not GREEN, but \nGREEN.

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.