Tuesday, 2 May 2017

Regex test always false on file nodejs

I have a log file where each line have this information:

[Thu Mar 30 2017 11:24:51 GMT+0100 (WEST)] {"serial":"CA-2M-1107619","type":"iface","body":{"action":"up","device":"tun_man","ip":"127.255.0.10","ip6":"2016:900d:c0de::1001"}

I want to parse the line and get the timestamp "Thu Mar 30 2017 11:24:51 GMT+0100 (WEST)", and the data between {}.

Anyone can help me with this? I need to split by " [] and {}" " to get what i want.

var lineReader = require('line-reader');
var Regex = require('regex');
var regexTimestamp = new Regex("/\w+\s\w+\s\d+\s\d+\s\d+:\d+:\d+\s\w+\+\d+\s\(\w+\)/");
var regexData = new Regex("/{.+}/");

var path = '/var/log/wscontroller_logevents1.log';
lineReader.open(path, function(err, reader) {
  if (err) throw err;
  if (reader.hasNextLine()) {
    reader.nextLine(function(err, line) {
      try {
        if (err) throw err;
        var testLine = '[Fri Apr 21 2017 13:09:51 GMT+0100 (WEST)] {"serial":"CA-2M-1128568","type":"iface","body":{"action":"ifupdate","device":"wan","ip":"","ip6":""}}';
        var testDate = '[Fri Apr 21 2017 13:44:53 GMT+0100 (WEST)]';
        var testData = '{"serial":"CA-2M-1128568","type":"iface","body":{"action":"ifupdate","device":"wan","ip":"","ip6":""}}';
        console.log('-----');
        console.log('-----');
        console.log('line', line);
        console.log('-----');
        console.log('-----');
        console.log('Date: ',regexTimestamp.test(testDate));
        console.log('Data: ',regexData.test(testData));
      } finally {
        reader.close(function(err) {
          if (err) throw err;
        });
      }
    });
  }
  else {
    reader.close(function(err) {
      if (err) throw err;
    });
  }
});



via Catia Matos

No comments:

Post a Comment