Created
January 7, 2016 21:23
-
-
Save danielledeleo/bb94849fdcb5657b8688 to your computer and use it in GitHub Desktop.
A little asynchronous multi-file grep
This file contains hidden or 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 littlemultifilegrep filename.txt term"'); | |
process.exit(1); | |
} | |
filenames = process.argv.slice(2, -1); | |
searchterm = process.argv.slice(-1); | |
search = 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]; | |
} | |
} | |
filenames.forEach(function(filename){ | |
fs.readFile(filename, 'utf-8', search); | |
}); | |
console.log('Results: '); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment