0

I have a node.js code that will read the log.txt. I have the simple code that is reading the file, but I want to find the occurrence of the text and show me the number of occurrences for each text.

log.text : 02/04/2020|abc,02/04/2020|123,02/04/2020|123,02/04/2020|bcd,02/04/2020|123,02/04/2020|abc

Output :

abc : 2
123 : 3
bcd : 1

CODE:

 // Read the file and print its contents.
    var fs = require('fs')
      , filename = process.argv[2];
    fs.readFile(filename, 'utf8', function(err, data) {
      if (err) throw err;
      console.log('OK: ' + filename);
      console.log(data)
    });
Noob_NoVoice
  • 217
  • 2
  • 10
  • 1
    Your question is not about reading a file, but to process the data read. You already have the text read into a string `data`. And now you need to process it. I would attempt first split it into elements (hint, use `data.split()`) and then collect all elements by name (hint, use `elements.reduce()`) – PA. Feb 04 '21 at 19:20
  • 1
    I agree with @PA, this is not a question about reading a file but about parsing a `string`. I would suggest you use regular expressions to count the number of occurrences of the text. Please [check this answer](https://stackoverflow.com/questions/1072765/count-number-of-matches-of-a-regex-in-javascript) for more information on how to do it. – guzmonne Feb 04 '21 at 19:33
  • And how do you break words? Are `,` and `|` delimiters? – Raeesaa Feb 05 '21 at 07:33

0 Answers0