Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 | egrep '(span class="body")|(span class="bodybold")' | sed -n '6p; 7p; ' | sed 's/\<*\>//g'


<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
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 | egrep 'span class="body' | sed -n '6,7{s/<[^>]*>//g;p}'

For OS X:

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

This worked for mjb:

curl --silent | 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 | 
tidy -q -c -wrap 0 -numeric -asxml -utf8 --merge-divs yes --merge-spans yes 2>/dev/null |
xmlstarlet sel -N x="" -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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .