title: Searching text file with readline node.js
body: <p>Please consider below code. What I want to do is to pick name from <code>names.txt</code> in <code>createStream</code> function one by one and from there call <code>FileSearch</code> function with <code>nameToSearch</code> as parameter and search <code>list.txt</code> line by line. So one name and search <code>list.txt</code> line by line, another name and search <code>list.txt</code> line by line and so on...</p>
  
  <p><code>list.txt</code> ,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</p>
  
  <ol>
  <li><p>Skipping rows. I searched some names manually and corresponding line numbers are not there in output)</p></li>
  <li><p>Mixing names. I can see names in the initial part of the output and then somewhere else in the output.</p></li>
  </ol>
  
  <p>Code:</p>
  
  <pre><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; 
  </code></pre>
  
  <p><code>names.txt</code> looks like this </p>
  
  <pre><code>OLIVER
  CHARLOTTE
  LIAM
  AMELIA
  .
  .
  </code></pre>
  
  <p>and <code>list.txt</code> looks like this, basically one word per line in both files</p>
  
  <pre><code>a
  aah
  aahed
  aahing
  aback
  abacus
  abacuses
  abaft
  abalone
  abalones
  .
  .
  </code></pre>
  
  <p>My out put on console is something like this </p>
  
  <pre><code>grayson:2322
  emmett:3756
  emmett:6399
  lily:3739
  lily:6340
  .
  .
  </code></pre>
  
  <p>Am I not using <code>readline</code> module correctly?</p>
  
  <p><strong>EDIT:</strong>  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 <code>list.txt</code>. I am not sure why</p>