Skip to content

Instantly share code, notes, and snippets.

@zeekrey
Created May 6, 2019 14:31
Show Gist options
  • Save zeekrey/8747b1dfa81c101e1e27ad40ec9969b1 to your computer and use it in GitHub Desktop.
Save zeekrey/8747b1dfa81c101e1e27ad40ec9969b1 to your computer and use it in GitHub Desktop.
Read and transform big file line by line with Node.js
var fs = require('fs'), es = require('event-stream')
// Define your input and output files
var inputFile = 'Path:/to/file'
var outputFile = 'Path:/to/file'
var lineNr = 0
var writeStream = fs.createWriteStream(outputFile)
var readStream = fs.createReadStream(inputFile)
readStream
.pipe(es.split())
.pipe(es.mapSync(function (line) {
// We will pause the reading to process our data
// The current processed line is in the 'line' variable
// We will resume processing after transformation
readStream.pause();
lineNr++;
transformAndWriteData(line, s.resume);
})
.on('error', function (err) {
console.log('Error while reading file.', err);
})
.on('end', function () {
console.log('Read entire file.')
})
);
function transformAndWriteData(data, callback) {
// You can do here what ever you want.
// It is possiblem to write the transformed line to a new file:
// Here I read a tab-splitted file, concat it with ';' and transform some json data
let n = data.split('\t')[0].concat(';', JSON.stringify(data.split('\t')[1]), '\n')
stream.write(n, function () {
callback()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment