Created
July 21, 2017 07:16
-
-
Save snoyberg/bd58030db9b9c90f9e1fcf8b31ea10e9 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env stack | |
-- stack script --resolver lts-8.12 | |
{-# LANGUAGE OverloadedStrings #-} | |
import Conduit | |
import Control.Monad | |
import Text.Regex (matchRegex,mkRegex,Regex) | |
import qualified Data.Text as T | |
loghead = mkRegex "^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} )" | |
-- "2015-01-25 00:04:18,840" | |
logMerge :: Monad m => Regex -> ConduitM String String m () | |
logMerge logregex = | |
loop "" | |
where | |
loop accum = do | |
mline <- await | |
case mline of | |
Nothing -> unless (null accum) (yield accum) | |
Just line -> | |
case matchRegex logregex line of | |
Just _ -> do | |
yield accum | |
loop line | |
Nothing | |
| null accum -> loop accum | |
| otherwise -> loop $ accum ++ "<br>" ++ line | |
runMerge :: String -> String -> IO () | |
runMerge infile outfile = | |
runConduitRes | |
$ sourceFile infile | |
.| decodeUtf8C | |
.| linesUnboundedC | |
.| mapC T.unpack | |
.| logMerge loghead | |
.| mapC T.pack | |
.| unlinesC | |
.| encodeUtf8C | |
.| sinkFile outfile | |
main :: IO () | |
main = runMerge "infile.txt" "outfile.txt" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment