Skip to content

Instantly share code, notes, and snippets.

@snoyberg
Created July 21, 2017 07:16
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 snoyberg/bd58030db9b9c90f9e1fcf8b31ea10e9 to your computer and use it in GitHub Desktop.
Save snoyberg/bd58030db9b9c90f9e1fcf8b31ea10e9 to your computer and use it in GitHub Desktop.
#!/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