Main Page |
Recipe 20.12 Parsing a Web Server Log File20.12.1 ProblemYou want to extract selected information from a web server log file. 20.12.2 SolutionPull apart the log file as follows: while (<LOGFILE>) { my ($client, $identuser, $authuser, $date, $time, $tz, $method, $url, $protocol, $status, $bytes) = /^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] "(\S+) (.*?) (\S+)" (\S+) (\S+)$/; # ... } 20.12.3 DiscussionThis regular expression pulls apart entries in Common Log Format, an informal standard that most web servers adhere to. The fields are listed in Table 20-1.
Other formats include the referrer and agent information. The pattern needs only minor changes to work with other log file formats. Beware that spaces in the URL field are not escaped, so we can't use \S* to extract the URL. .* would cause the regex to match the entire string and then backtrack until it could satisfy the rest of the pattern. We use .*? and anchor the pattern to the end of the string with $ to make the regular expression engine initially match nothing but then add characters until the entire pattern is satisfied. 20.12.4 See AlsoThe CLF spec at http://www.w3.org/Daemon/User/Config/Logging.html |
Main Page |