Last active
January 7, 2016 20:41
-
-
Save danielledeleo/9d8daa57b37f1ec8dbbc to your computer and use it in GitHub Desktop.
A little asynchronous grep
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
(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