Print the next X number of lines in Scala
- by soulesschild
Trying to learn Scala using the Programming in Scala book and they have a very basic example for reading lines from a file. I'm trying to expand on it and read a file line by line, look for a certain phrase, then print the next 6 lines following that line if it finds the line. I can write the script easily enough in something like java or Perl but I have no idea how to do it in Scala (probably because I'm not very familiar with the language yet...)
Here's the semi adapted sample code from the Programming in Scala book,
import scala.io.Source
if(args.length>0) {
val lines = Source.fromFile(args(0)).getLines().toList
for(line<-lines) {
if(line.contains("secretPhrase")) {
println(line)
//How to get the next lines here?
}
}
}
else
Console.err.println("Pleaseenterfilename")