I have a short script that I run at command line that searches a variable amount of files for a number and returns the first line number for each file it is found within.
How can I alter the output of the file to return a single text line of the results and write it to a file? As well as return the count of how many of the variable number of files had the number, to a second file?
Script
const readline = require("readline");
const fs = require("fs");
const SearchFiles = (readStream, filePath, queries) => {
let lineCount = 0;
let hold='';
let matches = new Map();
queries.forEach((query) => matches.set(query, []));
return new Promise((resolve, reject) => {
readStream.on("line", (line) => {
lineCount++;
for (let query of matches.keys()) {
if (searchForTerm(line, query))
{
matches.set(query, [...matches.get(query), lineCount]);
}}
});
readStream.on("close", () =>
resolve({
filePath,
matches,
})
);
});
};
const searchForTerm = (line, query) => line.match(query);
const createLineInterfaces = (filePaths) =>
filePaths.map((filePath) => {
const readStream = readline.createInterface({
input: fs.createReadStream(filePath),
});
return {
filePath,
readStream,
};
});
var lookup =56700;
var matchme =new RegExp("\\b" + lookup + "\\b");
const filesToSearch = ["/users/myname/desktop/mypath/threehundred_1191_37.txt", "/users/myname/desktop/mypath/threehundred_1191_37.txt", "/users/myname/desktop/mypath/threehundred_1191_36.txt", "/users/myname/desktop/mypath/threehundred_1191_35.txt", "/users/myname/desktop/mypath/threehundred_1191_38.txt", "/users/myname/desktop/mypath/threehundred_1191_39.txt", "/users/myname/desktop/mypath/threehundred_1191_40.txt", "/users/myname/desktop/mypath/threehundred_1191_41.txt"];
const queriesToSearch = [matchme];
let searchProms = createLineInterfaces(
filesToSearch
).map(({ readStream, filePath }) =>
SearchFiles(readStream, filePath, queriesToSearch)
);
Promise.all(searchProms).then((searchResults) =>
searchResults.forEach((result) => console.log(result))
)
Current Output
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_37.txt',
matches: Map(1) { /\b56700\b/ => [ 52313 ] }
}
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_36.txt',
matches: Map(1) { /\b56700\b/ => [ 52335 ] }
}
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_35.txt',
matches: Map(1) { /\b56700\b/ => [] }
}
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_38.txt',
matches: Map(1) { /\b56700\b/ => [ 52491 ] }
}
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_39.txt',
matches: Map(1) { /\b56700\b/ => [ 52392 ] }
}
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_40.txt',
matches: Map(1) { /\b56700\b/ => [ 52430 ] }
}
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_41.txt',
matches: Map(1) { /\b56700\b/ => [ 52450 ] }
}
{
filePath: '/users/myname/desktop/mypath/threehundred_1191_42.txt',
matches: Map(1) { /\b56700\b/ => [ 52425 ] }
}
Wanted Output written to a First file
yes yes non yes yes yes yes yes
Wanted Output written to a Second file
7
The yes/non string is in order that the files were given to search in the array. Yes means it is in file, non means it was not found.
Note I pretty much focus on PHP and python --- I just started nodejs 5 days ago and attempted this on my own with async/wait but it was no where as fast as this script... most likely because I have not mastered the .then, and promise.all with array_map.
I am at crunch time so the script above is being reused from this post below: How do I write the Regex for a Node app that acts like a simple JS linter with a condition as well