Skip to content

Instantly share code, notes, and snippets.

@danielledeleo
Last active January 7, 2016 21:14
Show Gist options
  • Save danielledeleo/8556a8c9cdbae2d8c2e7 to your computer and use it in GitHub Desktop.
Save danielledeleo/8556a8c9cdbae2d8c2e7 to your computer and use it in GitHub Desktop.
A little synchronous grep
(function() {
const fs = require('fs');
if (process.argv.length < 4) {
console.error('Not enough parameters given. Try this: "node findinfile_sync filename.txt term"');
process.exit(1);
}
const filename = process.argv[2];
const searchterm = process.argv[3];
const contents = fs.readFileSync(filename, 'utf-8');
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 above');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment