I am trying to come up with a way to delete a row in a CSV if it contains a certain value and not sure what structure is best to use?
I would like to read the file into memory and store it in a variable in a structure and then evaluate that structure and remove the desired rows.
Example:
aaa, 1, cat, car
bbb, 2, dog, truck
ccc, 1, bird, truck
ddd, 3, dog, car
eee, 3, mouse, car
fff, 2, cat, car
What I would want to do is remove the entire line if it contains a 2
So far I have tried placing it in a map, but I am not sure how to evaluate and remove the line. I would also be fine with simply not writing that line to the map.
class csvConsume {
static void main(args) {
def mapList = []
File csvFile = new File("testData.csv")
csvFile.eachLine { line ->
def parts = line.split(",")
def tmpMap = [:]
tmpMap.putAt("FirstCol", parts[0])
tmpMap.putAt("SecondCol", parts[1])
tmpMap.putAt("ThirdCol", parts[2])
tmpMap.putAt("FourthCol,", parts[3])
// etc.
mapList.add(tmpMap)
print(tmpMap)
}
}
}