Created
July 21, 2017 07:09
-
-
Save snoyberg/6537120fca2e9b8944e41fe60d285793 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 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