
cuboidgraphix
User
Jan 23, 2009, 1:54 PM
Post #1 of 6
(1724 views)
|
|
Question about parsing this line...
|
Can't Post
|
|
Hi again guys... Just got another question in regards to parsing a different kind of line. Just so let you guys know I really try parsing this file by myself and it has worked sort of but I know I'm not parsing it right. My question is... Can this be parsed using the Text::ParseWords?
2009-01-20 17:35:44 Local7.Notice 200.32.252.114 5247646: .Jan 20 23:35:08.271: %VOIPAAA-5-VOIP_CALL_HISTORY: CallLegType 2, ConnectionId AFE18BFE E68111DD B68ADBD7 E14468D4, SetupTime .17:34:16.701 CCT Tue Jan 20 2009, PeerAddress 667812818556529, PeerSubAddress , DisconnectCause 10 , DisconnectText normal call clearing (16), ConnectTime .17:35:08.271 CCT Tue Jan 20 2009, DisconnectTime .17:35:08.271 CCT Tue Jan 20 2009, CallOrigin 1, ChargedUnits 0, InfoType 2, TransmitPackets 1443, TransmitBytes 27321, ReceivePackets 949, ReceiveBytes 18551 2009-01-20 17:35:45 Local7.Notice 200.32.252.114 5247647: .Jan 20 23:35:09.783: %VOIPAAA-5-VOIP_CALL_HISTORY: CallLegType 1, ConnectionId C6510714 E68111DD B69CDBD7 E14468D4, SetupTime .17:34:54.333 CCT Tue Jan 20 2009, PeerAddress 1000, PeerSubAddress , DisconnectCause 10 , DisconnectText normal call clearing (16), ConnectTime .17:35:09.783 CCT Tue Jan 20 2009, DisconnectTime .17:35:09.783 CCT Tue Jan 20 2009, CallOrigin 2, ChargedUnits 0, InfoType 2, TransmitPackets 0, TransmitBytes 0, ReceivePackets 0, ReceiveBytes 0 I notice that the after a while it has (,) as the delimiter. If it is possible how would the delimiter look like? Below is my script that I have written so far. But chances are I would have to keep on adding to $DC. Is there a better way to parse it using the delimiter (,)?
#!/usr/bin/perl # This file is the parser.pl # This is a script that will parse data from the Syslog use strict; use warnings; use Text::ParseWords; my $file = $ARGV[0] || die "Usage: $0 <filename>\n"; open my $FH, '<', $file or die "Can't open '$file' $!"; while (<$FH>) { if($_ =~ /^20/){ my ($Local,$PA,$DC) = (quotewords('\s+', 0, $_))[2,24,28]; my ($calllog,$d1,$d2); if($Local =~ /^Local7.Notice/){ if($PA =~ /^501/){ $calllog = "ATT.txt"; } else{ $calllog = "MCI.txt"; } if($DC =~ /^3$|^1B|^26|^66/){ $d1 = 37; $d2 = 44; } if($DC =~ /^10|^12|^13|^64/){ $d1 = 36; $d2 = 43; } if($DC =~ /^1$|^11|^14|^15|^16|^1C|^1D|^1F|^22|^29|^2A|^2F|^58|^6F/){ $d1 = 35; $d2 = 42; } if($DC =~ /^7F/){ $d1 = 34; $d2 = 41; } if($DC =~ /^0$/){ $d1 = 33; $d2 = 40; } my ($Date,$Time,$CID1,$CID2,$CID3,$CID4,$CT, $DT) = (quotewords('\s+', 0, $_))[0,1,12,13,14,15,$d1,$d2]; my ($CID, $DIFF, $CALL, $secs1, $secs2); $CID = "$CID1 $CID2 $CID3 $CID4"; $CT = substr($CT,1,8); $DT = substr($DT,1,8); $secs1 = (substr($CT,0,2) * 3600)+(substr($CT,3,2) * 60)+substr($CT,6,2); $secs2 = (substr($DT,0,2) * 3600)+(substr($DT,3,2) * 60)+substr($DT,6,2); $DIFF = $secs2 - $secs1; print "Date: $Date \n"; print "Time: $Time \n"; print "ConnectionID: $CID \n"; print "PeerAddress: $PA \n"; print "ConnectTime: $CT \n"; print "DisconnectTime: $DT \n"; print "TotalTime: $DIFF \n"; print " \n\n"; $CALL = "$Date,$Time,$CID$PA$CT,$DT,$DIFF\n"; open my $CL, '>>', $calllog or die "Can't open '$calllog' $!"; print $CL $CALL; close $CL; } if($Local =~ /^Local7.Error/){ my $errorlog = "errorlog.txt"; open my $EL, '>>', $errorlog or die "Can't open '$errorlog' $!"; print $EL $_; close $EL; } } } close $FH; Thanks again for everything guys.
|