Searching text file with readline node.js

前端 未结 1 1437
青春惊慌失措
青春惊慌失措 2021-01-07 15:21

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 FileSear

相关标签:
1条回答
  • 2021-01-07 15:48

    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;
    
    0 讨论(0)
提交回复
热议问题