1

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
hussain
  • 6,587
  • 18
  • 79
  • 152
  • Have you tried Regular Expressions? Here is a link for JavaScript documentation of Regular Expressions. https://developer.mozilla.org/ro/docs/Web/JavaScript/Guide/Regular_Expressions – PianoSong Mar 31 '17 at 16:50
  • Is there some problem with just using grep, which has been able to do this in its sleep for at least 40 years. –  Mar 31 '17 at 17:56
  • @torazaburo i never used grep any reference or example ? – hussain Mar 31 '17 at 19:03
  • Umm, google "grep". –  Mar 31 '17 at 19:04
  • i also have merge task if second line dont have date merge with line beofore thats the end goal not sure if grep will help to achieve that as well. – hussain Mar 31 '17 at 19:05

2 Answers2

1

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.

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • i can use split package to achieve this task let me see – hussain Mar 31 '17 at 17:06
  • i added 2nd answer to my code using split and regex module its printing only `console.log('Line without date:', line)` its printing all lines – hussain Mar 31 '17 at 17:11
  • @hussain The regex may be too strict. See a 3rd example. I updated my answer. – rsp Mar 31 '17 at 17:13
  • so in third answer regex will not be function anymore it will throw exception – hussain Mar 31 '17 at 17:15
  • @hussain Yes, just remove the parentheses. See my updated answer. – rsp Mar 31 '17 at 17:28
  • Thanks that helped to print lines with date and without dates , I understand i might have to ask another question but just want to check if you can help. it is possible to merge if second line dont have date merge with first line and if third line dont have date merge with second. trying to put whole event into signle line. – hussain Mar 31 '17 at 17:44
  • @hussain Technically it's a different question but I may be able to post an answer if you ask about it. – rsp Mar 31 '17 at 18:00
  • let me know if you can answer otherwise i will ask another question – hussain Mar 31 '17 at 19:02
  • @hussain I can answer it but if you ask it as another question. The questions shouldn't be changed if they already have answers. But I will gladly answer another question if you post it. – rsp Mar 31 '17 at 19:41
  • i asked new question here http://stackoverflow.com/questions/43148701/how-to-remove-line-breaks-and-make-single-line-text – hussain Mar 31 '17 at 19:55
0

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);
});
John Vandivier
  • 2,158
  • 1
  • 17
  • 23