Skip to content

Instantly share code, notes, and snippets.

@fsalehpour
Last active November 2, 2017 10:05
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 fsalehpour/9e3802dee889169adc102c9f80c24bd1 to your computer and use it in GitHub Desktop.
Save fsalehpour/9e3802dee889169adc102c9f80c24bd1 to your computer and use it in GitHub Desktop.
regex to extract data from Apache's access log
<?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