Skip to content

Instantly share code, notes, and snippets.

@edvardm
Created November 4, 2011 20:42
Show Gist options
  • Save edvardm/1340426 to your computer and use it in GitHub Desktop.
Save edvardm/1340426 to your computer and use it in GitHub Desktop.
naive access log parser in haskell
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