Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^3: Splitting Apache Log Files

by crashtest (Curate)
on Apr 26, 2010 at 19:01 UTC ( #836968=note: print w/replies, xml ) Need Help??

in reply to Re^2: Splitting Apache Log Files
in thread Splitting Apache Log Files

qr// is a regular expression quote, and as such does, in a sense, compile regular expressions. Unfortunately, you're using the regular expression as a hash key, at which point it's turned back into a string. As you process the Apache log file, $rule is just a string. When you use it as a regular expression, it has to be compiled again - each time through the loop.

If I were writing your code, I would store the regular expression rules/filehandles in an array. Here's a sketch of what it might look like:

my @rules; # not %rules. ... # Process input file of processing rules while(<INFILE>) { ... push @rules, { regex => qr/$string/, file_handle => $fh }; } ... # Read Apache log file and print to various other files while (my $line = <STDIN>) { for my $rule_ref (@rules){ my $regex = $rule_ref->{regex}; my $fh = $rule_ref->{file_handle}; if ($line =~ $regex) { print $fh $line; } } }

Hope this helps.

Replies are listed 'Best First'.
Re^4: Splitting Apache Log Files
by cmm7825 (Novice) on Apr 26, 2010 at 20:16 UTC
    WOW, never knew that. Thanks a lot the execution time went done to 22seconds.
      execution time went done to 22seconds.

      You must have a pretty damn fast disk. SSD?

      I split a 500MB file into 10 on the basis of a single digit at a fixed position in each line:

      while( <> ) { print {$fhs[ substr $_, 2, 1 ]}, $_; }

      And I can't get below 1 minute.

      If you're managing to test each line against many (how many?) regexes, and still beat mine by 60%, I want a disk like yours.

      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.
        I'm not sure the exact specs...but its a production server. I assume it has RAID and SCSI hardisks
      Just as a simple I/O test, I played around with just creating a 500MB file and then copying that file to another file on my old WinXP machine.
      #!/usr/bin/perl -w use strict; my $begin = time(); open (BIG, ">fiveHundredMB") or die "cannot open file for 500MB write" +; my $c255 = '*'x255; my $c256 = "$c255\n"; my $oneK = "$c256"x4; my $oneMB = "$oneK"x1024; print BIG $oneMB for (1..500); close BIG; my $end = time(); print "elasped time for 500MB file is: ", $end-$begin, " seconds\n"; __END__ elasped time for 500MB file is: 9 seconds I opened this file in my text editor and there are 2,048,000 lines of 256 chars = 524,288,000 bytes. Windows says: 526,336,000 bytes at command line. This difference is a mystery to me at the moment. But this is basically a ~500 MB file.
      #!/usr/bin/perl -w use strict; my $begin = time(); open (BIG, "<fiveHundredMB") or die "cannot open file for 500MB read"; open (OUT, ">bigfile") or die "cannot open bigfile for write"; while (<BIG>) { print OUT $_; } close OUT; my $end = time(); print "elasped time for 500MB file is: ", $end-$begin, " seconds\n"; __END__ prints: elasped time for 500MB file is: 13 seconds
      Even at 22 seconds, the execution time seems slow, but that depends upon the number of regex'es you are running per line of input and how many lines that there are. I suspect that they are far less than 1024 bytes in length on average. If your performance is adequate for your use, I would stick a fork in it and call it done! I wouldn't worry about it. About 13-14 seconds is as fast as a single HD can go without any processing of the data.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://836968]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-03-23 16:56 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (290 votes). Check out past polls.