http://www.perlmonks.org?node_id=873736


in reply to how to quickly parse 50000 html documents?

For counterpoint, I ran your snippet through the following one liner and got pretty much what you need:

>perl -nle"m[<font size=1>([^<]+)</font></td></tr>] and print $1" junk +.txt 936 20 4 506 502 6 2.76 14 27 76 581 (11.4%) 19,021 843 (90.1%) $257,524 $90,945 48 2,602 118

Your description says each page consists of two same-sized set of these values, so just discard the first half. You don't want the dollar signs, commas or percentages, so post process to remove them.

People will tell you that this is fragile, and will break if the page is changed. But any solution will break if the pages change, but given how simple this is, it'll will probably be quicker to fix this, than any solution that relies upon fuzzy parsing of a whole heap of stuff that you have no interest in whatsoever.

Just as I don't bother reading the stories on the newspaper my fish&chips comes wrapped in before eating; I don't bother parsing a bunch of html I've no interest in. Ie. Don't parse; simply extract.

It will certainly be a whole heap faster. Given your description of the size of the files, I estimate that the above should be able to process each page in about 1/20th of a second, giving you a total time of about 40 minutes instead of your current 28hrs.

Update: I revise my estimate to just over 3 minutes based upon running this code:

#! perl -nlw use strict; use Time::HiRes qw[ time ]; BEGIN{ @ARGV = map glob, @ARGV } local $/; my $start = time; while( <> ) { my @vals; while( m[<font size=1>([^<]+)</font></td></tr>]g ) { my $val = $1; $val =~ tr[$,][]d; $val =~ s[^\s*([0-9.]+).+$][$1]e; push @vals, $val; } print "@vals[ @vals /2 .. $#vals ]"; } print time-$start;

Over 1000 copies of a mocked up file containing 10 copies of your snippet (5 as the reference; 5 as the wanted) in 4 seconds:

C:\test>873713 junk*.txt ... 93 2 4 50 50 6 2.7 1 2 7 581 1902 843 25752 9094 4 260 93 2 4... 93 2 4 50 50 6 2.7 1 2 7 581 1902 843 25752 9094 4 260 93 2 4... 93 2 4 50 50 6 2.7 1 2 7 581 1902 843 25752 9094 4 260 93 2 4... 93 2 4 50 50 6 2.7 1 2 7 581 1902 843 25752 9094 4 260 93 2 4... 4.07200002670288 ^Z

Even if the page layout changes, the 27 hrs 57 minutes you saved each time you need to do this, should cover the 5 minutes it will take to re-write it :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.