Scala Process with pipe -
Scala Process with pipe -
i trying run bash command 1 in scala:
cat "example file.txt" | grep abc
scala has special syntax process piping, first approach:
val filename = "example file.txt" (process(seq("cat", filename)) #| process(seq("grep", "abc"))).run()
as far understand, executes first process, reads output scala , feeds sec process. problem that, performance reasons, execute both processes without leaving terminal. file huge , don't need whole output, that's why using grep in first place. so, sec approach one:
val filename = "example file.txt" (process(seq("bash", "-c", "cat " + filename + " | grep abc"))).run()
the problem here breaks if filename has spaces. seek escape spaces, rather have scala doing me (there many other characters need escape).
is there way run command?
it easy plenty escape filename:
val escapedfilename = "'" + filename.replace("'", "'\\''") + "'"
but right way pass filename straight grep:
process(seq("grep", "abc", filename)).run()
which equivalent in shell:
grep abc "example file.txt"
scala process escaping
Comments
Post a Comment