From 8e39d38f76071e902cb171fe5e30e0a6cfff587a Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 30 Oct 2018 22:32:38 -0400 Subject: [PATCH] Add grep command --- src/grep.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/grep.hs diff --git a/src/grep.hs b/src/grep.hs new file mode 100644 index 0000000..f1e9a80 --- /dev/null +++ b/src/grep.hs @@ -0,0 +1,20 @@ +module Main where + +import Data.List (isInfixOf) +import System.Environment (getArgs) + +-- grep +main :: IO() +main = do + args <- getArgs + + --Get the search pattern from the first argument + let pattern = head args + + --Read all file names on command line + fileContents <- mapM readFile $ tail args + + let lineContents = concat $ map lines fileContents + let selection = filter (isInfixOf pattern) lineContents + + mapM_ putStrLn selection