#!/usr/bin/perl -w use strict; use warnings; use Date::Parse; while (defined (my $line = )) { # format: remotehost login authuser [date] "request" status bytes "Referer" "Agent" # example: 127.0.0.1 - - [27/Jan/2011:10:14:37 +1100] "GET /reports HTTP/1.1" 301 311 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.14eol) Gecko/20060311 Epiphany/1.8.5" my @fields = $line =~ m{\A(\S+) (\S+) (\S+) \[(.*?)\] "(.*?)" (\S+) (\S+) "(.*?)" "(.*?)"\n\z}s; if (!@fields) { die "can't parse line $.: $line\n"; } my ($remotehost, $login, $authuser, $date, $request, $status, $bytes, $referer, $agent) = @fields; my ($method, $url, $proto) = $request =~ m{\A(\S+) (.*?) (\S+)\z}; if (!defined $method) { die "can't parse request in line $.: $line\n"; } my ($path, $query) = $url =~ m{\A([^?]*)(\?.*|)\z}; if (!defined $path) { die "can't parse url in line $.: $line\n"; } my $date1 = $date; for ($date1) { s{/}{ }g; s/:/ /; } my $time = str2time($date1); my ($ss, $mm, $hh, $day, $month, $year, $zone) = strptime($date1); $year += 1900; $month += 1; # print $time, "\n"; my $date2 = sprintf("%04d-%02d-%02d %02d-%02d-%02d", $year, $month, $day, $hh, $mm, $ss); print "$date2\t$path\n"; # print join(" | ", @fields), "\n"; }