Skip to content

Instantly share code, notes, and snippets.

@curtislacy
Created August 20, 2013 18:33
Show Gist options
  • Save curtislacy/6285360 to your computer and use it in GitHub Desktop.
Save curtislacy/6285360 to your computer and use it in GitHub Desktop.
Simple node.js script that takes input, splits it into lines, and does something with it (removes/counts duplicates, in this case).
process.stdin.resume();
process.stdin.setEncoding('utf8');
var lineCounts = {};
var previousFragment = '';
process.stdin.on('data', function (chunk) {
var lines = chunk.split( '\n' );
lines[0] = previousFragment + lines[0];
previousFragment = lines.pop();
lines.forEach( function( line ) {
if( lineCounts.hasOwnProperty( line ))
lineCounts[ line ] ++;
else
{
lineCounts[ line ] = 1;
}
});
});
process.stdin.on( 'close', function() {
var total = 0;
var unique = 0;
for( var k in lineCounts )
{
if( lineCounts.hasOwnProperty( k ))
{
total += lineCounts[ k ];
unique ++;
console.log( k );
}
}
/* console.log( 'Total lines: ' + total );
console.log( 'Total unique lines: ' + unique );
console.log( 'Number of occurances by line: ' );
console.log( lineCounts );*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment