How the Scala script that reads 5G log file from network drive should be modified in order to read last x lines (like 'tail' in Unix)?

StackOverflow https://stackoverflow.com/questions/1669277

  •  13-09-2019
  •  | 
  •  

Question

How the Scala script that reads 5G log file from network drive should be modified in order to read last x lines (like 'tail' in Unix)?

::#!
@echo off
call scala %0 %*
goto :eof
::!#

import scala.io.Source
if (args.length > 0) {
for (line <-Source.fromFile(args(0)).getLines)
if(line.contains("percent")){
    print(line)
}
}
Was it helpful?

Solution

I'm using a mutable queue in this one:

::#!@echo off
call scala %0 %*
goto :eof
::!#
import scala.io.Source

val lastN = 5 // I guess you'll be getting them from args, but...
val queue = new scala.collection.mutable.Queue[String]

if (args.length > 0) {
  Source.fromFile(args(0)).getLines foreach { line =>
    queue.enqueue(line)
    if (queue.size > lastN) queue.dequeue
  }
  for (line <- queue)
    if (line.contains("percent")){
      print(line)
    }
}

If using an immutable queue, I'd use a reduceLeft, but I see no point in using an immutable queue for this.

OTHER TIPS

If reading the file is expensive, as I expect it is over the network, I'd seek to the end of the file and read progressively larger chunks (more domain knowledge of the log file format might give you a better strategy here) from the end until you find the number of lines you're looking for.

You'll obviously have to keep a buffer of x lines which you update on each iteration:

var buf: List[String] = Nil

for (line <- ...) {
  buf = (buf ::: List(line)) match {
    case x :: xs if (xs.length == n) => xs 
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top