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.

I'm trying to sanitize this output from it's metadata to plug this output into GreekTools, but I am getting stuck on sed.

curl --silent www.brainyquote.com | egrep '(span class="body")|(span class="bodybold")' | sed -n '6p; 7p; ' | sed 's/\<*\>//g'

[ex]

<span class="body">Literature is news that stays news.</span><br> <span class="bodybold">Ezra Pound</span>

Could someone help me along on this track?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You should really use a proper HTML or XML parsing tool. Trying to parse it with regular expressions leads to madness.

However, for simple cases:

curl --silent www.brainyquote.com | egrep 'span class="body' | sed -n '6,7{s/<[^>]*>//g;p}'

For OS X:

curl --silent www.brainyquote.com | egrep 'span class="body' | sed -n '6,7{' -e 's/<[^>]*>//g' -e 'p' -e '}'

This worked for mjb:

curl --silent www.brainyquote.com | egrep '(span class="body")|(span class="bodybold")' | sed -n '6p; 7p; ' | sed -e 's/<[^>]*>//g'
share|improve this answer
    
Voting up not because the regex works (bc it doesn't), but bc you pointed me to that madness article and it made my morning :) –  mjb Mar 13 '11 at 15:41
    
@mjb: I'm sorry, I had a typo. Please see my edited answer. –  Dennis Williamson Mar 13 '11 at 15:50
    
@Dennis - I still end up w/an error: sed: 1: "6,7{s/<[^>]*>//g;p}": extra characters at the end of p command –  mjb Mar 13 '11 at 17:14
    
@mjb: Oh, sorry, I wasn't paying attention to the fact that you're using OS X. Try: sed -n -e '6,7{s/<[^>]*>//g' -e 'p}' –  Dennis Williamson Mar 13 '11 at 19:59
    
my mistake - I didn't know the syntactic relevance of osx to the Q. It still hates the brackets - sed -n -e '6,7{s/<[^>]*>//g' -e 'p}' sed: 1: "p}\n": extra characters at the end of p command –  mjb Mar 13 '11 at 20:22

Just for completeness, a solution using HTML tidy and xmlstarlet:

# note: use recent versions of tidy and xmlstarlet
curl -s www.brainyquote.com | 
tidy -q -c -wrap 0 -numeric -asxml -utf8 --merge-divs yes --merge-spans yes 2>/dev/null |
xmlstarlet sel -N x="http://www.w3.org/1999/xhtml" -T -t -m "//x:td[@align='center' and @valign='top' and @width='300']/x:span[@class='body']" -v '.' -n \
-m "//x:html/x:body/x:div/x:table/x:tr[position()=2]/x:td[@align='center' and @valign='top' and @width='300']/x:span[@class='bodybold']" -v '.' -n
share|improve this answer
    
Thanks very much for an XML based solution. Yikes, even command-line XML looks thorny! ;-) –  shellter Apr 8 '11 at 1:27

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.