Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl string data split

Posted on 2007-12-03
8
Medium Priority
?
4,742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Accepted Solution

by:
Adam314 earned 200 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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

609 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