Perl string data split

Hi all this is my first question on this board.
I have a data log file which needs splitting.
the data file consists of the following data:
Fri Nov 30 16:52:25 2007 | <hostname> | <ip_address> |<user>|logon failed |
Mon Dec  3 09:59:25 2007 | <hostname> | <ip_address> |<user>|logon succeeded |

I have separated this pipe delimited file. However I need to separate date field (first column) to separate

the problem is the $date can either be single or double characters & my perl has gone rusty after being away from it for 5 years. Is there a simple solution that will split Fri Nov 30 16:52:25 2007 or  Mon Dec  3 09:59:25 2007 to separate $day,$month,$date,$time,$year reliably?
Who is Participating?
Adam314Connect With a Mentor Commented:
The problem is there are 2 spaces between month and date when the date is a single digit.  You need the regex in split to take multiple spaces as a single separator.

my @datetime= split(/ +/,$date);    #Notice the +

Open in new window

while( <DATA> ){
 my($day,$month,$date,$time,$year) = split;
  print "day=$day,month=$month,date=$date,time=$time,year=$year\n";
Fri Nov 30 16:52:25 2007 | <hostname> | <ip_address> |<user>|logon failed |
Mon Dec  3 09:59:25 2007 | <hostname> | <ip_address> |<user>|logon succeeded |
you can try modules like POSIX::strptime


use POSIX::strptime;
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = POSIX::strptime("Fri Nov 30 16:52:25 2007", "%a %b %d %H:%M:%S %Y");
print "$sec $min  $hour $mday $mon  $year  $wday $yday";

Open in new window

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

1TsuppAuthor Commented:
Thanks for that. I dont't have POSIX::strptime installed on this solaris 10 system.
I've tried splitting the logfile as thus:-
while (<LOG>)
        my @line = split(/\|/, $_);
        print "log data pipe split:-$line[0]::$line[1]::$line[2]::$line[3]::$line[4]\n";  
        my $date=$line[0];
        my @datetime= split(/ /,$date);

With this for double date digits @datetime gets the correct split but for single digits it doesn't.
I've tried ozo's but that doesnt work either.
what was wrong with http:/#a20394928 ?
what was wrong with http:#a20394928 ?
1TsuppAuthor Commented:
hi ozo i tried your suggestion but it didn't work & with my bit code adam314 suggestion worked.
thanks for the suggestion.
What does "didn't work" mean?
When I run it, it prints
Did it do something different for you, or were you expecting it to do something else?
All Courses

From novice to tech pro — start learning today.