I am rather new to nodejs. I am able to read line by line and print to console but cannot loop through the text file like in vbscript or python. Here's what I want to do.
Loop through a file for a word. Once I locate the word have the program go to the text file's next line and print it out to screen and the next line and so on until I get to an empty line(that's where I will stop the loop).
Right now all that happens is it reads the word and stops. I have not found a way online to loop though a text file in nodejs.
I am lost on how get to the next line in the text file.
Please explain thoroughly how to get around this current problem I have. What else can be add to get the lineReader to go to the next line. the code is below. It has been cleaned up. I tried to do another for-loop inside the if statement but it doesn't work. I tried Array but it will not work either.
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('text.txt')
});
lineReader.on('line', function (line) {
for (var x= 0; x < line.length; x++)
if(line === "word"){
console.log(line);
}
}
});
Here is the revised version. I modified spooky's example.
function find(){
var Arry = [];
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(text.txt)
});
lineReader.on('line', function (line) {
Arry.push(line);
});
lineReader.on('close', function (line) {
var Ncount = 0;
for(var x =0; x < Arry.length; x++){
if (Arry[x] == 'cat'){
do {
Ncount +=1;
x +=1;
if (Arry[x] == "") {
Ncount = 700;
}
console.log(Arry[x]);
} while (Ncount<700 );
}
}
});
}