On comparing different files
import sys.process._"
"diff data.txt myFile.txt" !
the exit code proves nonzero as expected,
< 1
---
> 123
res1: Int = 1
However on trying to gather the actual differences
val d = "diff data.txt myFile.txt" !!
java.lang.RuntimeException: Nonzero exit value: 1
at scala.sys.package$.error(package.scala:27)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.slurp(ProcessBuilderImpl.scala:132)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang$bang(ProcessBuilderImpl.scala:102)
... 33 elided
To ask how to fetch the differences into a String
(given small files) ?
I think that you need to use the lineStream_!
method, and not the !
. With the line stream which is the output of the diff
command, you can do whatever you want: print it, convert it to list, to array, or whatever else you need.
val st = "diff input.txt output.txt" lineStream_!
// now st is a Stream[String]
println(st.mkString("\n"))
EDIT: I believe that method was called before lines_!
. Now it's deprecated. Just in case you are using an old Scala version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With