Last active
January 7, 2016 21:14
-
-
Save danielledeleo/8556a8c9cdbae2d8c2e7 to your computer and use it in GitHub Desktop.
A little synchronous 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 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