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?

link|improve this question

feedback

2 Answers

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'
link|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
show 3 more comments
feedback

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
link|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
feedback

Your Answer

 
or
required, but never shown

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