2

I recently used event-stream library for nodejs to parse a huge csv file, saving results to database. How do I solve the task of not just reading a file, but modifying each line, writing result to new file? Is it some combination of through and map method, or duplex? Any help is highly appreciated.

Dominik Domanski
  • 1,023
  • 2
  • 10
  • 18

1 Answers1

3

If you use event-stream for read you can use split() method process csv line by line. Then change and write line to new writable stream.

var fs = require('fs');
var es = require('event-stream');

const newCsv = fs.createWriteStream('new.csv');

fs.createReadStream('old.csv')
  .pipe(es.split())
  .pipe(
    es.mapSync(function(line) {
    // modify line way you want
      newCsv.write(line);
    }))

newCsv.end();
l2ysho
  • 2,542
  • 1
  • 20
  • 41