Dear Perl Monks, I am developing at the moment a script which has to parse 20GB files. The files I have to parse are some logfiles. My problem is that it takes ages to parse the files. I am doing something like this:
my %lookingFor; # keys => different name of one subset # values => array of one subset my $fh = new FileHandle "< largeLogFile.log"; while (<$fh>) { foreach my $subset (keys %lookingFor) { foreach my $item (@{$subset}) { if (<$fh> =~ m/$item/) { my $writeFh = new FileHandle ">> myout.log"; print $writeFh <$fh>; } } }
I've already tried to speed it up by using the regExp flag=>o by doing something like this:
$isSubSet=buildRegexp(@allSubSets); while (<$fh>) { foreach my $subset (keys %lookingFor) { if (&$isSubSet(<$fh>)) { my $writeFh = new FileHandle ">> myout.log"; print $writeFh <$fh>; } } } sub buildRegexp { my @R = @_; my $expr = join '||', map { "\$_[0] =~ m/\(To\|is\)\\:\\S\+\\@\$R[$_ +]/io" } ( 0..$#R ); my $matchsub = eval "sub { $expr }"; if ($@) { $logger->error("Failed in building regex @R: $@"); return ERROR; } $matchsub; }
I don't know how to optimize this more. Maybe it would be possible to do something with "map"? I think the "o" flag didn't speed it up at all. Also I've tried to split the one big file into a few small ones and use some forks childs to parse each of the small ones. Also this didn't help. Thanks a lot for your help! -Marco

In reply to how to parse large files by domcyrus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.