Reputation: 7083
I want to print lines that contains date in the string , i am using split module to achieve that task. below code is always printing else statement.
ctrl.js
fs.readFile(dir + '/' + logFile, 'utf8', function(err, data) {
var lines = data.split('\n');
var linesWithDate = lines.split('|')[0].replace(/[\[\]']+/g,'');
lines.forEach(function(line) {
if (linesWithDate) {
console.log('print lines with date',line);
} else {
console.log('print lines without date',line);
}
}
});
file data
[2017-03-23T18:13:16Z]|zlpv7490|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event lorem ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy
[2017-03-23T19:20:16Z]|zlpv7490|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event lorem ipsum
Upvotes: 1
Views: 2007
Reputation: 111336
How to print lines that contains specific string?
const split = require('split');
fs.createReadtStream(path.join(dir, logFile), 'utf8')
.pipe(split()).on('data', (line) => {
if (line.indexOf('string') > -1) {
console.log('Line with string:', line);
} else {
console.log('Line without string:', line);
}
});
I want to print lines that contains date in the string , i am using split module to achieve that task. below code is always printing else statement
I don't think you were using the split
module. This example does:
const split = require('split');
const regex = require('regex-iso-date');
fs.createReadtStream(path.join(dir, logFile), 'utf8')
.pipe(split()).on('data', (line) => {
if (regex().test(line)) {
console.log('Line with date:', line);
} else {
console.log('Line without date:', line);
}
});
Note that this will not necessarily be a valid date, as it may match dates like 2017-13-13... - to test for valid dates only see this answer:
Or if you want to match for your specific strings like [2017-03-23T18:13:16Z]
then you may try something like this:
const split = require('split');
const regex = /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\w+\]/;
fs.createReadtStream(path.join(dir, logFile), 'utf8')
.pipe(split()).on('data', (line) => {
if (regex.test(line)) {
console.log('Line with date:', line);
} else {
console.log('Line without date:', line);
}
});
Note that it will also match invalid dates if you have them in your files.
Upvotes: 1
Reputation: 2426
Here is some code which prints out only valid parsed dates by line. I use the readline module instead of readfile, but you should be able to easily adapt the logic by replacing input: process.stdin
:
'use strict';
var sExample = '[2017-03-23T18:13:16Z]|zlpv7490|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event lorem ipsum';
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.setPrompt('Enter example string to test > ');
rl.prompt();
rl.on('line', (line) => {
var sCandidate = line.split('|')[0].replace(/[\[\]']+/g,'');
if (Date.parse(sCandidate)) { //it is a valid date
console.log(sCandidate);
}
process.exit(0);
});
Upvotes: 0