Created
April 1, 2016 11:46
-
-
Save yoava/7d45c531cf727247c393d43d6033a938 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sort2 uses sort-stream, as suggested by Joe Krill | |
////////////////////////////////////////////////////////////////////////////////////// | |
var fs = require('fs'); | |
var sort = require('sort-stream'); | |
var parse = require('csv-parse'); | |
var transform = require('stream-transform'); | |
var start = Date.now(); | |
// Create a readble stream from the input file. | |
var pipe = fs.createReadStream('./input.txt') | |
// Use `csv-parse` to parse the input using a tab character (\t) as the | |
// delimiter. This produces a record for each row which is an array of | |
// field values. | |
.pipe(parse({ | |
delimiter: '\t' | |
})) | |
// Use `sort-stream` to sort the parsed records on the third field. | |
.pipe(sort(function (a, b) { | |
return a[2].localeCompare(b[2]); | |
})) | |
// Use `stream-transform` to transform each record (an array of fields) into | |
// a single tab-delimited string to be output to our destination text file. | |
.pipe(transform(function (row) { | |
return row.join('\t') + '\r'; | |
})) | |
// And finally, output those strings to our destination file. | |
.pipe(fs.createWriteStream('./out2.txt')); | |
pipe.on('finish', function () { | |
var closetime = Date.now(); | |
console.log('Read entirefile. ', (closetime - start)/1000, ' secs'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment