Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to extract the string which are starting with the tag <span class="style530"> and ending with </span> tag.

I used sed command but didn't get desired result. Below is sample code:

<strong>
-
<span class="style530">
AA - 
This
is my
First
Heading</span></strong><br>
<span class="style530">
<strong>
*Some
text,*
<strong>
*text*</strong>, 
*text*
<strong>
*text*</strong>: 
<br>
<span class="style530">
<strong>
- This 
is my
Second Heading</strong></span><br>
<span class="style530">
<strong>
*Some
text,*
<strong>
*text*</strong>, 
*Here
is some
text.*
<strong>*text*</strong>: 
*Here is 
some
text*.<br>
<br>
<strong>
-
<span class="style530">
- This is
my Third
Heading</span></strong><br>

Output should be like:

 AA - This is my First Heading
 - This is my Second Heading
 - This is my Third Heading

Thanks!

share|improve this question
2  
The standard warning here is you cannot reliably parse HTML with regular expressions, so if this is for full pages whose content may vary, you should use an HTML parser. However, if it is just a short, predictable segment like this you may be able to do something. –  goldilocks May 5 at 14:17
    
Can you provide more concise input and show what have you done? –  Gnouc May 5 at 14:41
    
Also your example and requirement doesn't match your output, there is more text under <span class="style530"> tags than just the headings you note and your html is invalid or incomplete –  mtm May 5 at 14:46
    
Thanks for answer! @mtm the code is like that only, only few <span class="style530"> tags is closing with </span> and i need to extract only those strings which are closing with tag </span> –  user66895 May 6 at 8:57

3 Answers 3

Regex isn't really capable of parsing html completely.

There is a command line tool called xidel that lets you use XPath or CSS selectors to pull out the bits you want.

Something like this would meet your stated requirement:

./xidel test.html --extract '//span[@class="style530"]' --output-format bash

But note that this returns more than your required output as you have one unclosed <span class="style530">

share|improve this answer

Use HTMLParser for such actions:

#!/usr/bin/python
# vim: set fileencoding=utf8 :
# (c) fazie

from HTMLParser import HTMLParser
import re
import sys

class MyParser(HTMLParser):
    inside_span = False

    def __init__(self,file):
        HTMLParser.__init__(self)
        f = open(file)
        self.feed(f.read())

    def handle_starttag(self,tag,attrs):
        if tag == 'span':
            for name,value in attrs:
                if name=='class' and value=='style530':
                    self.inside_span=True

    def handle_data(self,data):
        data = data.strip(' \t\r\n')
        if data != "":
            if self.inside_span:
                data = re.sub('\n',' ',data)
                data = re.sub('\s\s+',' ',data)
                print data

    def handle_endtag(self,tag):
        if tag == 'span':
            self.inside_span=False

MyParser(sys.argv[1])

Run it:

python myparser.py inputfile.html
share|improve this answer

You can try something like below.

awk -vRS='<' '
  inside || /^span[^>]*class="style530"/ {
    inside = 1
    if (/^span/)
      n++
    else if (/^\/span>/ && !--n) {
      $0="/span>\n"
      inside=0
    }
    printf "<%s", $0
  }' file.html | sed '/^</ d' | grep -v ">$"

However, it is not advisable to extract using HTML headers. Please see here for why you should not parse HTML pages. I would suggest you to use curl and w3m to remove the HTML headers after which the parsing will become little simpler.

share|improve this answer
    
Thanks! I'm getting all strings which are not required... as i need only that string which are closing with the tag </span> –  user66895 May 6 at 9:05

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.