Last active
November 2, 2017 10:05
-
-
Save fsalehpour/9e3802dee889169adc102c9f80c24bd1 to your computer and use it in GitHub Desktop.
regex to extract data from Apache's access log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pattern = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) \- \- \[([^\]]+)\] "([A-Z]+) ([^" ]+) [^"]*" ([0-9]+) ([0-9]+) "([^"]*)" "([^"]*)"/'; | |
$heads = 'IP, timestamp, method, URI, status, size, referrer, agent' . PHP_EOL; | |
fputs(STDOUT, $heads); | |
while ($entry = fgets(STDIN)) { | |
preg_match($pattern, $entry, $segments); | |
array_shift($segments); | |
fputcsv(STDOUT, $segments); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment