Skip to content

Instantly share code, notes, and snippets.

@corpix
Created March 7, 2012 21:05
Show Gist options
  • Save corpix/1996250 to your computer and use it in GitHub Desktop.
Save corpix/1996250 to your computer and use it in GitHub Desktop.
Count lines with length >= than in a file
// node count-lines.js "somefile.txt" 12
var fs = require('fs')
, length = process.argv.pop()
, filename = process.argv.pop()
, stream
, counter = 0;
stream = fs.createReadStream(filename);
stream.on('data', function(lines){
if(!lines)
return;
lines = lines.toString().split('\n');
for(var key in lines){
if(lines[key].length >= length)
counter++;
}
delete lines;
});
stream.on('end', function(){
console.log('Lines with length %s -> %s', length, counter);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment