Sunday, February 13, 2011

Small Groovy Script to filter a file

Groovy makes life sooo easy...

this small script prints only lines who do not start with file:///

I know it's much simpler in grep, but try doing something more complex with grep and awk and you will waste the entire day...


package com.pierre

datafile = new File('C:/pierre/download/myfile.txt') 
outdatafile = new File('C:/pierre/download/myfile.txt.out')
PrintWriter pw = new PrintWriter(outdatafile)

datafile.eachLine{ 
 line -> 
 if (!line.startsWith("file:///")) {
  pw.write(line)
  pw.write("\n") 
 } 
}


No comments: