Print the next X number of lines in Scala

Posted by soulesschild on Stack Overflow See other posts from Stack Overflow or by soulesschild
Published on 2012-11-30T22:59:27Z Indexed on 2012/12/02 23:04 UTC
Read the original article Hit count: 154

Filed under:

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")

© Stack Overflow or respective owner

Related posts about scala