title: loop through a text file in nodejs
body: <p>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.</p>
  
  <p>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).</p>
  
  <p>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. </p>
  
  <p>I am lost on how get to the next line in the text file.</p>
  
  <p>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. </p>
  
  <pre><code>var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('text.txt')
  });
  
  
  lineReader.on('line', function (line) {
     for (var x= 0; x &lt; line.length; x++)
       if(line === "word"){
  
          console.log(line);
       }
  
     }
  
  
  });
  </code></pre>
  
  <p><strong>Here is the revised version. I modified spooky's example</strong>.</p>
  
  <pre><code>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 &lt; Arry.length; x++){
        if (Arry[x] == 'cat'){
  
        do { 
             Ncount +=1;    
             x +=1;
             if (Arry[x] == "") {
              Ncount = 700;
             }
           console.log(Arry[x]);
        } while (Ncount&lt;700 );
  
  
      }
  
    }
  
  
   });
  
  }
  </code></pre>