Solved

Perl string data split

Posted on 2007-12-03
8
4,733 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
what was wrong with http:/#a20394928 ?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
what was wrong with http:#a20394928 ?
0
 

Author Comment

by:1Tsupp
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

13 Experts available now in Live!

Get 1:1 Help Now