Skip to content

Instantly share code, notes, and snippets.

@veltman
Created November 13, 2015 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/0156943a5b037cda6e44 to your computer and use it in GitHub Desktop.
Save veltman/0156943a5b037cda6e44 to your computer and use it in GitHub Desktop.
var fs = require("fs"),
split = require("split"),
through2 = require("through2");
var destinations = {};
fs.createReadStream("all.txt",{encoding:"utf8"})
.pipe(split())
.pipe(through2({decodeStrings:false},transform,flush));
function transform(row, enc, callback) {
getDestination(row[3]).write(row + "\n","utf8",callback);
}
function flush() {
// unnecessary?
for (var char in destinations) {
destinations[char].end();
}
}
function getDestination(char) {
if (!destinations[char]) {
destinations[char] = fs.createWriteStream(char + ".txt");
}
return destinations[char];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment