Created
January 31, 2013 09:19
-
-
Save wavewave/4681580 to your computer and use it in GitHub Desktop.
simple replace string in a file
This file contains hidden or 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
| {-# LANGUAGE OverloadedStrings #-} | |
| import Control.Applicative | |
| import Data.Attoparsec | |
| import qualified Data.Attoparsec.ByteString.Char8 as AC | |
| import qualified Data.ByteString as B | |
| import qualified Data.ByteString.Char8 as C | |
| import Data.List | |
| import Data.Monoid | |
| import System.Environment | |
| import System.IO | |
| -- | |
| import Prelude hiding (take) | |
| main :: IO () | |
| main = do | |
| args <- getArgs | |
| if length args /= 4 | |
| then putStrLn "simplereplace originalstr newstr srcfile tgtfile" | |
| else do | |
| bstr <- B.readFile (args !! 2) | |
| let r = parseOnly (findparser (C.pack (args !! 0))) bstr | |
| case r of | |
| Left err -> print err | |
| Right lst -> let nlst = intersperse (C.pack (args !! 1)) lst | |
| in B.writeFile (args !! 3) (mconcat nlst) | |
| findparser :: B.ByteString -> Parser [B.ByteString] | |
| findparser bstr = do | |
| strs <- many $ manyTill AC.anyChar (try (AC.string bstr)) | |
| str <- manyTill AC.anyChar endOfInput | |
| return $ (map C.pack strs) ++ [C.pack str] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment