Skip to content

Instantly share code, notes, and snippets.

@viatsko
Created August 27, 2017 22:42
Show Gist options
  • Save viatsko/c939c505a9242313d0278829d1cee88c to your computer and use it in GitHub Desktop.
Save viatsko/c939c505a9242313d0278829d1cee88c to your computer and use it in GitHub Desktop.
Node.JS - reading log line-by-line
// https://stackoverflow.com/a/23695940/844204
var fs = require('fs')
, util = require('util')
, stream = require('stream')
, es = require('event-stream');
var lineNr = 0;
var s = fs.createReadStream('very-large-file.csv')
.pipe(es.split())
.pipe(es.mapSync(function(line){
// pause the readstream
s.pause();
lineNr += 1;
// process line here and call s.resume() when rdy
// function below was for logging memory usage
logMemoryUsage(lineNr);
// resume the readstream, possibly from a callback
s.resume();
})
.on('error', function(err){
console.log('Error while reading file.', err);
})
.on('end', function(){
console.log('Read entire file.')
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment