In my implementation, if a user searches from a client, I want to check if the string is part of the file. I am sending that line data back to the client as a response.
My issue is lets assume that a user wants to search for testid012
. So with my current code, it only finds a singular line that contains the search string. Instead I want to send a response with multiple lines that include the search string, which in this case is testid012
. Is this doable?
searchService.js
fs.readFile('logs/dit/' + logfile.filename, 'utf8', function (err, data) {
if (err) {
return done(err);
}
var lines = data.split('\n'); // get the lines
lines.forEach(function(line) { // for each line in lines
if (line.indexOf(searchStr) != -1) { // if the line contain the searchSt
results.push({
filename:logfile.filename,
value:line
});
}
});
// when you are done reading the file
done();
});
dit/server.log
testid012 Lorem test Ipsum is simply dummy text text of the printing and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry
Lorem test Ipsum is simply dummy text text of the printing and typesetting industry,testid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry
testid013 Lorem test Ipsum is simply dummy text text of the printing and typesetting industry,testid Lorem test Ipsum is simply dummy text text of the printing and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry
Lorem test Ipsum is simply dummy text text of the printing and typesetting industry
Your solution will work, but the reason you are only getting one line is your are splitting your input by line end. Seems like you need to figure out how your events are separated and change your data.split()
call.
If there is data that starts every event like [EVENT] This is an event
then use a regex to split on that. data.split('[EVENT] ')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.