4

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 );


    }

  }


 });

}
Nb_me
  • 231
  • 2
  • 6
  • 20
  • 3
    Possible duplicate of [Read a file one line at a time in node.js?](http://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js) – LBes Aug 22 '16 at 21:48
  • not a duplicate. The other post does not address my question – Nb_me Aug 22 '16 at 21:56
  • well you wanted a node.js way of reading a file line by line no? – LBes Aug 22 '16 at 22:02
  • yes...only to get the file into the program. I want to loop through the file and then print line by line until a condition is met – Nb_me Aug 22 '16 at 22:04
  • Which is what is done in the answer provided with the link to the possible duplicate if I'm not wrong... – LBes Aug 22 '16 at 22:07

3 Answers3

3

Use this code which uses Array to be able to go back and forth within data. Beware this can be memory intensive but helps with you cause:

var fileLineArray = [];
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('text.txt')
});

lineReader.on('line', function (line) {
//      console.log(line);
        fileLineArray.push(line);

});

lineReader.on('close', function(){
        for(var i=0; i<fileLineArray.length;i++){
             var wordIs = fileLineArray[i];
             if(wordIs === "word"){
                console.log(wordIs);
                }
        }
});

Old answer was: Use this code:

var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('text.txt')
});


lineReader.on('line', function (line) {
//      console.log(line);
     if(line === "word"){ //I am checking for word "word"

        console.log(line);
     }

});
spooky
  • 1,620
  • 1
  • 13
  • 15
  • The text.txt contains words one word on each line.. If you have multiple words on each line then you will have to implement tokenizer. – spooky Aug 22 '16 at 21:59
  • It does what it should read each line but I can not manipulate in a way such as to go to the next line and read it once the line containing the word is found. – Nb_me Aug 22 '16 at 22:10
  • @Nb_me I now understand what you are trying to do... May be you should read the file into an array and then manipulate as you wish? Have you tried that? – spooky Aug 22 '16 at 22:36
  • @Nb_me I have updated my answer according to your needs... Good luck my friend. – spooky Aug 22 '16 at 22:45
  • thanks man....I revised it to fit my needs. I'm still not clear on what is going on with using the 'on' twice but I am new so I have to learn it as I go. – Nb_me Aug 23 '16 at 14:32
3

This is what I was wanting. 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 );


    }

  }


});

}
Nb_me
  • 231
  • 2
  • 6
  • 20
1

If you look at the last example here: https://nodejs.org/api/readline.html

const readline = require('readline');
const fs = require('fs');

const rl = readline.createInterface({
  input: fs.createReadStream('sample.txt')
});

rl.on('line', (line) => {
  console.log('Line from file:', line);
});

For each line, the function will be called with the line variable and the log will be triggered, you can wrap the console.log in your conditional. The thing to remember here is that the function you provide will be called as a callback with line passed in as a parameter so there is no need for external looping.

  • Ok...Help me to understand more. Here is an example of what I want to do. The file contains a word say "cat". the if statement finds this. now there are 20 lines of text until there is a line with no text(empty. this is where I want to stop the print out). you said wrap the console.log in my condition. the console.log only prints where the variable is if the condition is met. How can I go to the top and read beyond where it line variable has stopped. – Nb_me Aug 22 '16 at 22:27
  • Conceivably you could set an external (global) variable like print = false, to true on your start condition and only print when it is true, i.e. set it to false on your end condition. This seems like the naive solution to me I am sure there is a more elegant way to do it. But right now strikes me as the easiest way. – KARLockhart Aug 23 '16 at 01:53
  • @KARLockhart...Thanks I used spooky's example. The code is up top near my original post. – Nb_me Aug 23 '16 at 15:09