Current location: Hot Scripts Forums » Programming Languages » Perl » Parse the log file


Parse the log file

Reply
  #1 (permalink)  
Old 11-08-10, 11:52 PM
szm1996 szm1996 is offline
New Member
 
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow Parse the log file

Hi,

I am new to perl programming and could use a little beginners advice. I have this file that contains date, email address and text.

101020 16:35:07 test1@name.xyz.com Account tested
101020 16:42:27 admin@name.xyz.com password reset requested
101020 16:42:39 fname@domanin.net password reset enabled

Using perl programming is it possible to read this file and split the date in each line to have date, domain address and activity message. The ouput may be similar to as below

101020 16:35:07
test1@name.xyz.com
Account tested

similarly for other lines as well.

Thanks,
Reply With Quote
  #2 (permalink)  
Old 11-09-10, 01:16 PM
jrl657 jrl657 is offline
New Member
 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Hope this helps. I am new here and my first post. There is many ways to get this done this is one and it would be my quick down and dirty method.

$infile="textin.txt"; #name of file with the input text in it
open(IN,"<$infile") or die "cant open $infile:$!";

while(<IN>) {
## Next lines shows how matches are marked ##
## | $1 | | $2 | | $3 | | $4 | ##
m/^(\d{6})\s(\d{2}\:\d{2}\:\d{2})\s(.*\@.*?)\s(.*)$/;

#$1 is match from "\d{6}" ie 6 digits
$date = $1;
#$2 is match from "\d{2}\:\d{2}\:\d{2}" ie 2digits:2digits:2digits
$time = $2;
#$3 is match from ".*\@.*?" ie any char any number of times@any char any number of
### times non greedy
$email = $3;
#$4 is match from ".*" ie any char any number of times
$activity = $4;

print "$date $time\n$email\n$activity\n\n";
}

close(IN);
Reply With Quote
Reply

Bookmarks

Tags
cgi, perl, regular expression


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse log file and insert data in MYSQL szm1996 Script Requests 0 11-08-10 05:10 PM
Generating a very basic log file of data insert macintosh PHP 2 05-10-10 08:32 AM
perl file to parse eddiet610 Perl 0 04-14-10 05:34 AM
create log file rsmahaa ASP 1 03-03-10 02:31 PM
Hello all, can you help me? K4ot1K Other Languages 2 01-23-09 05:23 AM


All times are GMT -5. The time now is 04:55 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.