title: Output the answer once when done, not on every line
body: <p>I run the following script and it prints the output for every line in file, but I need output the answer once when done, not on every line. Anyone please help me</p>
  <pre><code>const fs = require(&quot;fs&quot;);
  const readline = require(&quot;readline&quot;);
  const stream = require(&quot;stream&quot;);
  
  const filename = process.argv[2];
  const instream = fs.createReadStream(filename);
  const outstream = new stream();
  outstream.readable = true;
  outstream.writable = true;
  
  const rl = readline.createInterface({
    input: instream,
    output: outstream,
    terminal: false,
  });
  const obj = {};
  rl.on(&quot;line&quot;, function (line) {
    const [country] = line.split(&quot;,&quot;, 1);
    {
      for (let i = 0; i &lt; [country].length; i++)
        if (obj[country] != null) {
          obj[country] += 1;
        } else {
          obj[country] = 1;
        }
    }
    console.log(obj);
  });
  </code></pre>