I am trying to write a simple node program that reads a csv file, extracts a column (say second) and writes it to another CSV file. I am reading the contents to an array and then writing that array to file.
Stages and data in each step
inputfile
123,456,789,abc
def,ghi,232,jkl
array
['456','ghi']
outputfile
4,5,6
g,h,i
output needed
456
ghi
Am i just missing a configuration or is the way i am writing data wrong? Is my block of code to write to file within END block not correct?
Here is my code
var fast_csv = require('fast-csv');
var tempArray=new Array();
console.log("START");
fast_csv.fromPath("inputfile.txt").on("data", function(data){
tempArray.push(data[1]);
})
.on("end", function(){
tempArray.sort();
console.log(tempArray);
fast_csv.writeToPath("outputfile.csv", tempArray)
.on("finish", function(){
console.log("END");
});
});