Skip to content

Instantly share code, notes, and snippets.

@snoyberg
Created July 21, 2017 07:09
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/6537120fca2e9b8944e41fe60d285793 to your computer and use it in GitHub Desktop.
Save snoyberg/6537120fca2e9b8944e41fe60d285793 to your computer and use it in GitHub Desktop.
#!/usr/bin/env stack
-- stack script --resolver lts-8.12
{-# LANGUAGE OverloadedStrings #-}
import Conduit
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
:: Regex
-> Maybe String -- ^ new line
-> String -- ^ accumulator
-> (String, [String])
logMerge _ Nothing accum = ("", [accum ++ "\n"])
logMerge logregex (Just str) accum =
case matchRegex logregex str of
Just _ -> (str,[(accum++"\n")])
Nothing -> case null accum of
True -> (str,[])
False -> (accum ++ "<br>" ++ str,[])
runMerge :: String -> String -> IO ()
runMerge infile outfile =
runConduitRes
$ ((sourceFile infile .| decodeUtf8C .| linesUnboundedC .| mapC (Just . T.unpack)) >> yield Nothing)
.| concatMapAccumC (logMerge loghead ) ""
.| mapC T.pack
.| 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