Skip to content

Instantly share code, notes, and snippets.

@kanatohodets
Created February 4, 2014 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanatohodets/7b3d3188a3588241ffc1 to your computer and use it in GitHub Desktop.
Save kanatohodets/7b3d3188a3588241ffc1 to your computer and use it in GitHub Desktop.
Parsing nginx access log -- failed match?
use v6;
my $line = '137.22.189.191 - - [02/Feb/2014:23:37:36 -0600] "GET / HTTP/1.1" 200 669 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36"';
grammar AccessLogParser {
rule TOP { ^ <log_line> $ }
rule log_line {<ip_address> ' - - '<date> <request>}
rule request { <method> <url> <protocol> <response_code> <bytes_sent> <referrer> <user_agent> }
token ip_address { $<ip_address> = [\d ** 3\.\d ** 2\.\d ** 3\.\d ** 3]
{ say "got IP: $<ip_address>"; }}
token date {$<date> = \[(.+)\] {say "found a date? $<date>";}}
token method {...}
token url {...}
token protocol {...}
token response_code {...}
token bytes_sent {...}
token referrer {...}
token user_agent {...}
}
say AccessLogParser.parse($line);
#output:
#got IP: 137.22.189.191
##<failed match>
say $line ~~ m/\[(.+)\]/;
#output:
#「[02/Feb/2014:23:37:36 -0600]」
# 0 => 「02/Feb/2014:23:37:36 -0600」
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment