Groovy Bar & Grill

August 29, 2006

Groovy Log Splitter

Filed under: Groovy Grill — jawild59 @ 9:46 pm

Matt and Blaine are having fun writing Ruby log splitters. I couldn’t pass up the opportunity to write one in Groovy, so here it is:

  baseFilename = args.size() ? args[0"split.log"
  maxLines = args.size() == ? args[1500000
  fileCount = 0
  lineCount = 0
  inputFile = new File(baseFilename)
  writer = newWriter(baseFilename)
  inputFile.eachLine {
    if (lineCount >= maxLines) {
      writer = newWriter(baseFilename)
    }
    writer.append("${it}\n")
    lineCount++
  }
  
  def newWriter(filename) {
    lineCount = 0
    new File("${fileCount++}_${filename}")
  }

1 Comment »

  1. Awesome! I actually like this better than mine…. more succint. Now, lets see your take on the DSL version.

    Comment by Matt Secoske — August 29, 2006 @ 10:12 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.