Skip to content

Instantly share code, notes, and snippets.

@takungsk
Created April 6, 2012 11:15
Show Gist options
  • Save takungsk/2318941 to your computer and use it in GitHub Desktop.
Save takungsk/2318941 to your computer and use it in GitHub Desktop.
テキストファイルを読み込んで 指定した文字列を含む行を出力するサンプル
import scala.io.Source
object CheckLog {
case class Args(
filename: String,
exp: String
)
def main(args: Array[String]) {
val argsOption = parseArgs(args.toList)
val s = Source.fromFile(argsOption.get.filename,"UTF8")
val p = argsOption.get.exp.r
try {
for (line <- s.getLines) {
var i = i + 1
val l = p.findFirstMatchIn(line)
l.foreach{ lf => println("*** " + lf.matched + " *** " + "Line:" + i + " " + lf.source)}
}
} finally {
s.close
}
}
def parseArgs(args: List[String]): Option[Args] = {
var fileName = "./takuya71.log"
var exp = ".*error|hoge"
@scala.annotation.tailrec
def parseArgsHelper(args: List[String]): Args = {
args match {
case "-f" :: x :: rest => {
fileName = x
parseArgsHelper(rest)
}
case "-e" :: x :: rest => {
exp = x
parseArgsHelper(rest)
}
case _ => Args(fileName,exp)
}
}
try {
Some(parseArgsHelper(args))
}
catch {
case _ => None }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment