I have used http://www.regexe.com/ to test a regex I've create in order to extract the date and time from syslogand it's showing me that the regex is in fact correct, highlighting the date and time. However when I try this in Perl I'm returned with just the time, not the date.
so for example from the string Dec 9 12:45:36 osboxes NetworkManager[739]: <info> address 192.168.10.129
I would be returned 12:45:36
Here's my script:
use strict;
use warnings;
my $keywords = 'keywords.txt';
open(my $kw, '<:encoding(UTF-8)', $keywords)
or die "Could not open file '$keywords' $!"; # Open the file, throw an exception if the file cannot be opened.
chomp (my @keywordsarray = <$kw>); # Remove whitespace, and read it into an array
close($kw);# Close the file
my $syslog = 'syslog';
open(my $sl, '<:encoding(UTF-8)', $syslog)
or die "Could not open file '$keywords' $!"; # Open the file, throw an exception if the file cannot be opened.
chomp (my @syslogarray = <$sl>); # Remove whitespace, and read it into an array
close($sl);# Close the file
foreach my $line (@syslogarray)
{
foreach my $keyword (@keywordsarray)
{
if ($line =~ m/\Q$keyword\E/)
{
if ((my $date) = $line =~ m/[A-z]+\s{2}\d{1,}\s((\d{2}[:]){2}\d{2})/)
{
print "**". $keyword. "**". $date. "\n";
}
}
}
}