Skip to content

Instantly share code, notes, and snippets.

@andreruffert
Created November 18, 2015 20:01
Show Gist options
  • Save andreruffert/f4d0dd7385e2735eca93 to your computer and use it in GitHub Desktop.
Save andreruffert/f4d0dd7385e2735eca93 to your computer and use it in GitHub Desktop.
Node.js read file line by line and transform it
const fs = require('fs');
const readline = require('readline');
const readFile = readline.createInterface({
input: fs.createReadStream('file.in'),
output: fs.createWriteStream('file.out'),
terminal: false
});
readFile
.on('line', transform)
.on('close', function() {
console.log(`Created "${this.output.path}"`);
});
function transform(line) {
this.output.write(`modified ${line}\n`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment