Searching text file with readline node.js

Please consider below code. What I want to do is to pick name from names.txt in createStream function one by one and from there call FileSearch function with nameToSearch as parameter and search list.txt line by line. So one name and search list.txt line by line, another name and search list.txt line by line and so on...

list.txt ,is a large file. If finds a match, output the search string and line number. I thought it'll out put one name and line numbers and then move on to second name and so on but it is

  1. Skipping rows. I searched some names manually and corresponding line numbers are not there in output)

  2. Mixing names. I can see names in the initial part of the output and then somewhere else in the output.

Code:

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

var nameToSearch

var createStream = function (){
var lineNumber=0;
var ended = false;
var rlName = readline.createInterface({
      input : fs.createReadStream('./names.txt'),
      output: process.stdout,
      terminal: false
      })
rlName.on('line',function(lineInNameFile){
     ++lineNumber;
      nameToSearch=lowercase(lineInNameFile);
     FileSearch(nameToSearch);
     })
}


var lowercase=function(str){
     return String(str).toLowerCase();
}

var  FileSearch = function (searchString){
var listlineNumber=0;
var rlList = readline.createInterface({   
      input : fs.createReadStream('./list-2.txt'),
      output: process.stdout,
      terminal: false
})
rlList.on('line',function(lineInListFile){
     ++listlineNumber;
     if (lineInListFile===searchString){
         console.log( searchString+ ":" + listlineNumber);
     } 

})

} 

var runSearch = createStream();
exports.createStream = createStream; 

names.txt looks like this

OLIVER
CHARLOTTE
LIAM
AMELIA
.
.

and list.txt looks like this, basically one word per line in both files

a
aah
aahed
aahing
aback
abacus
abacuses
abaft
abalone
abalones
.
.

My out put on console is something like this

grayson:2322
emmett:3756
emmett:6399
lily:3739
lily:6340
.
.

Am I not using readline module correctly?

EDIT: upon looking into more and searching found that it might have to do something with reading last line. Its not reading the last line of list.txt. I am not sure why

Answers:

Answer

I manage to resolve the issue by using line-by-line module and now the output is exact as expected. heres the code

var nameToSearch

//Function to retrieve names

var createStream = function (){

var lineNumber=0;
var nameLine = require('line-by-line')
lrName = new nameLine('./names.txt');
//lrName = new nameLine('./names-testFile.txt');

var ended = false;

 lrName.on('line', function (lineInNameFile) {
    ++lineNumber;
    nameToSearch=lowercase(lineInNameFile);
    FileSearch(nameToSearch);
 });     


}


var lowercase=function(str){
     return String(str).toLowerCase();
}

 //Function to search in list file

var  FileSearch = function (searchString){

    var LineByLineReader = require('line-by-line'),
    lr = new LineByLineReader('./list.txt');
    //lr = new LineByLineReader('./list-testFile.txt');
    var listlineNumber=0
    var arr = []

    lr.on('line', function (lineInListFile) {
        ++listlineNumber;
        if (lineInListFile===searchString){
            arr.push(listlineNumber);

        } 
     });

     lr.on('end', function () {
            console.log( searchString+ ":" + arr);
     });

} 

var runSearch = createStream();

exports.createStream = createStream;

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.