Reading multiple files and extracting 1st column using scala -
i starting learn scala , got simple problem. used doing using unix command line bash , awk decided use scala learning.
i want parse multiple text file tab separated , want extract 1 or arbitrary column.
i want remove lines start "#" able do.
the code below print first row specific column each file. how print rows?
import scala.io.source  if (args.length > 0){      (arg<-args){         val file= source.fromfile(arg).getlines.filter(s => !(s contains "#")).mkstring("\n").split("\t")         println(file(2))         } }  else console.err.println("please enter filename")   thank you
calling mkstring("\n") on getlines result in single string of entire file , reason seeing output first row alone.
the following code snippet should working:
  if (args.length > 0) {     (arg <- args) {       println(source.fromfile(arg).getlines().filternot(_.trim.startswith("#")).map(_.split("\t")(2)).mkstring("\n"))     }   }      
Comments
Post a Comment