Store the result of the shell as an int in a variable

So, I work with files, and I need to know the largest line in the X file. Using Unix awk leads to the Int I'm looking for. But in Haskell, how can I return this value and store it in a variable?
I tried to identify something withIO [Int] -> [Int]

maxline = do{system "awk ' { if ( length > x ) { x = length } }END{ print x }' filename";}

does not work:

Couldn't match expected type 'Int',against inferred type 'IO GHC.IO.Exception.ExitCode'
+5
source share
2 answers

This is because the action systemreturns the completion status of the executed command, which cannot be converted to Int. You must use readProcessto output commands.

> readProcess "date" [] []
  "Thu Feb  7 10:03:39 PST 2008\n"

Note that it readProcessdoes not pass the command to the system shell: it runs it directly. The second parameter is the arguments to the command. Therefore your example should be

readProcess "awk" [" { if ( length > x ) { x = length } }END{ print x }", "/home/basic/Desktop/li11112mp/textv"] ""
+7

readProcess, . IO String String; , String , IO String s. IO ( ) - sigfpe, ! (, , .) IO Monad , .

pure-Haskell ( awk). readFile, lines maximumBy.

+6

All Articles