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
Skipping rows. I searched some names manually and corresponding line numbers are not there in output)
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