line_count.js 409 B

1234567891011121314151617181920
  1. #!/usr/bin/env node
  2. var argv = require('optimist')
  3. .usage('Count the lines in a file.\nUsage: $0')
  4. .demand('f')
  5. .alias('f', 'file')
  6. .describe('f', 'Load a file')
  7. .argv
  8. ;
  9. var fs = require('fs');
  10. var s = fs.createReadStream(argv.file);
  11. var lines = 0;
  12. s.on('data', function (buf) {
  13. lines += buf.toString().match(/\n/g).length;
  14. });
  15. s.on('end', function () {
  16. console.log(lines);
  17. });