Skip to content

Instantly share code, notes, and snippets.

@danielledeleo
Last active January 7, 2016 20:41
Show Gist options
  • Save danielledeleo/9d8daa57b37f1ec8dbbc to your computer and use it in GitHub Desktop.
Save danielledeleo/9d8daa57b37f1ec8dbbc to your computer and use it in GitHub Desktop.
A little asynchronous grep
(function() {
const fs = require('fs');
if (process.argv.length < 4) {
console.error('Not enough parameters given. Try this: "node littlegrep_async filename.txt term"');
process.exit(1);
}
filename = process.argv[2];
searchterm = process.argv[3];
fs.readFile(filename, 'utf-8', function(err, contents) {
var buffer = '';
for(i = 0; i<contents.length; i++) {
if(contents[i] === '\n') {
if(buffer.indexOf(searchterm) > -1) {
console.log(buffer);
}
buffer = '';
continue;
}
buffer += contents[i];
}
});
console.log('Results: ');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment