Solved

Perl string data split

Posted on 2007-12-03
8
4,735 Views
Last Modified: 2012-05-05
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
$day,$month,$date,$time,$year.

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?
0
Comment
Question by:1Tsupp
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20394928
while( <DATA> ){
 my($day,$month,$date,$time,$year) = split;
  print "day=$day,month=$month,date=$date,time=$time,year=$year\n";
}
__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 |
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20394974
you can try modules like POSIX::strptime

eg

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

0
 

Author Comment

by:1Tsupp
ID: 20395479
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>)
      {            
        chomp($_);
        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.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 50 total points
ID: 20396107
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 20397635
what was wrong with http:/#a20394928 ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 20397637
what was wrong with http:#a20394928 ?
0
 

Author Comment

by:1Tsupp
ID: 20401745
hi ozo i tried your suggestion but it didn't work & with my bit code adam314 suggestion worked.
thanks for the suggestion.
0
 
LVL 84

Expert Comment

by:ozo
ID: 20401783
What does "didn't work" mean?
When I run it, it prints
day=Fri,month=Nov,date=30,time=16:52:25,year=2007
day=Mon,month=Dec,date=3,time=09:59:25,year=2007
Did it do something different for you, or were you expecting it to do something else?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now