Thursday, 8 June 2017

How to match multiple string keywords when you read each line of file (fs) and get the results?

Basically i have mutiple search strings from client that i have to find in each line in the file , if line matches with all the strings from user input push that line to results, with below code its sending empty array of results.

How can i loop through each search string and find a match and once loops is done push all matching lines to results.

searchLogs.js

/* Example client input   
    var searchStr = [{"text":"457383",{"text":"requestPdr"}]
    */
async.eachSeries(filesData.logFiles, function(logfile, done) {
        // read file
        readStream = fs.createReadStream('./logs/' + filesData.searchEnv + '/' + logfile.filename, 'utf8')
        readStream.pipe(split())
            .on('data', function(line) {
                for (var i = 0; i < searchStr.length; i++) {
                    if (line.toLowerCase().indexOf(searchStr[i].toLowerCase()) != -1)
                        results.push({
                            filename: logfile.filename,
                            value: line
                        });

                }
            });

    },
    function(err) {
        if (err) {
            console.log('error', err);
        }
        readStream.on('end', function() {
            callback(results);
        });
        results = [];
    });



via hussain

No comments:

Post a Comment