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
Solved

Perl string data split

Posted on 2007-12-03
8
4,738 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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
 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Perl efficient DB Call 8 93
perl to convert excel to csv 3 253
Log File Creation with Header and Footer 17 127
perl syntax 3 16
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

856 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