Skip to content

Instantly share code, notes, and snippets.

@kellyrob99
Created December 4, 2011 18:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellyrob99/1430935 to your computer and use it in GitHub Desktop.
Save kellyrob99/1430935 to your computer and use it in GitHub Desktop.
OpenCSV in a groovy script to read in a file, filter the content and write out the result
@Grab(group = 'net.sf.opencsv', module = 'opencsv', version = '2.3')
import au.com.bytecode.opencsv.CSVReader
import au.com.bytecode.opencsv.CSVParser
import au.com.bytecode.opencsv.CSVWriter
def TEST_FILE_NAME = 'test.csv'
def TEST_OUTPUT_FILE_NAME = 'testOut.csv'
List<String[]> rows = new CSVReader(new FileReader(new File(TEST_FILE_NAME)), CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_ESCAPE_CHARACTER, CSVParser.DEFAULT_QUOTE_CHARACTER, 1).readAll()
def rowsOver100 = rows.findAll {it[1].toInteger() > 100}
File output = new File(TEST_OUTPUT_FILE_NAME)
if (output.exists())
{
output.delete()
}
output.createNewFile()
output.withWriter { writer ->
new CSVWriter(writer).writeAll(rowsOver100)
}
//println rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment