Created
November 4, 2011 20:42
-
-
Save edvardm/1340426 to your computer and use it in GitHub Desktop.
naive access log parser in haskell
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
import Data.List | |
import Text.ParserCombinators.Parsec | |
w3cLog = endBy line eol | |
line = do | |
[date, time] <- count 2 $ prg field | |
[ip, meth, uri, status, bytes, elapsedms] <- count 6 $ prg field | |
[referer, ua] <- count 2 $ prg quotedField | |
cookie <- quotedField | |
let datetime = intercalate " " [date, time] | |
return (datetime, ip, meth, uri, int status, int bytes, int elapsedms, referer, ua, cookie) | |
int s = read s :: Int | |
csep = char '\t' | |
field = many (noneOf "\t\n") | |
prg f = do | |
v <- f | |
csep | |
return v | |
quotedField = | |
do char '"' | |
content <- many1 quotedChar | |
char '"' <?> "quote at end of field" | |
return content | |
quotedChar = noneOf "\"" <|> try (string "\"\"" >> return '"') | |
eol = try (string "\n") <?> "end of line" | |
main = | |
do c <- getContents | |
case parse w3cLog "(stdin)" c of | |
Left e -> do putStrLn "Error parsing input:" | |
print e | |
Right r -> mapM_ print r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment