Wednesday, October 21, 2009

Read file by blocks

Perl is very good at reading files lines by lines, using the new line as a separator. You can still define the separator character, by setting the '$/' variable (you can find an example here).
However, when you are reading a log file, with some logs on several lines, the only solution is to parse the text. But here, split comes to the rescue, and you can easily parse.

For instance, if every log is starting with something like '2009/10/21 15:12:01.123 ', and of course if no other line can start with that pattern, here is an easy way :

my $buf="";
local $/=undef;
local *FILE;
open(FILE,"<${file}") or die "Couldn't open file: $!";
$buf = ;
close FILE;

my @logs=split(/(20[0-9]{2}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2} /,$buf);
my $nbLogs=scalar(@logs)/2;

This is perhaps not the more refined way to do it, but it does the job, and it does it well.