Skip to content

Instantly share code, notes, and snippets.

@larsen
Created February 14, 2012 11:59
Show Gist options
  • Save larsen/1826313 to your computer and use it in GitHub Desktop.
Save larsen/1826313 to your computer and use it in GitHub Desktop.
sub parse_log_line {
my $log_line = shift;
my $monthname_numbers = {
Jan => "01", Feb => "02", Mar => "03", Apr => "04",
May => "05", Jun => "06", Jul => "07", Aug => "08",
Sep => "09", Oct => "10", Nov => "11", Dec => "12"
};
if ($log_line =~ /^(.*?) - - \[(.*)\] "(.*)" (\d+) \d+ "(.*)" "(.*)" hostname ".*?"/) {
my $ip_address = $1;
my $date = $2;
my $query_string = $3;
my $status = $4;
my $referer = $5;
my $useragent = $6;
# $date is like "19/Apr/2011:06:54:51 +0000"
my ($dmy, $hh, $mm) = split /:/, $date;
my ($dd, $month_name, $yyyy) = split m[/], $dmy;
my $ymd = join "", ( $yyyy, $monthname_numbers->{ $month_name }, $dd );
return ( $ip_address, $ymd, $query_string, $status, $referer, $useragent, $hh, $mm );
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment