CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Question about parsing this line...

 



cuboidgraphix
User

Jan 23, 2009, 1:54 PM

Post #1 of 6 (2402 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?


Code
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 (,)?


Code
#!/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.


KevinR
Veteran


Jan 23, 2009, 2:37 PM

Post #2 of 6 (2396 views)
Re: [cuboidgraphix] Question about parsing this line... [In reply to] Can't Post

If the comma can be used as the delimiter you would just use a comma:


Code
my @fields = quotewords(',', 0, $_);

-------------------------------------------------


cuboidgraphix
User

Jan 23, 2009, 2:41 PM

Post #3 of 6 (2395 views)
Re: [KevinR] Question about parsing this line... [In reply to] Can't Post

Sorry for the ignorance.. but like my dad always said..

He who asks is a fool for a minute, but he who does not ask remains a fool...

Anyways... can there be two delimiters like on excel? Where the delimiter is space but also a comma whenever there's one?

I don't know if you understand what I'm asking.

:(


KevinR
Veteran


Jan 23, 2009, 3:03 PM

Post #4 of 6 (2392 views)
Re: [cuboidgraphix] Question about parsing this line... [In reply to] Can't Post

Yes there can be two or more delimiters but then the line will be split on each delimiter you define. Thats probably not what you want to happen. If you did want to split on the spaces (or multiple spaces) and the commas you can try this:


Code
my @fields = quotewords('[\s,]+', 0, $_);

-------------------------------------------------


(This post was edited by KevinR on Jan 23, 2009, 3:07 PM)


cuboidgraphix
User

Jan 25, 2009, 2:18 PM

Post #5 of 6 (2371 views)
Re: [KevinR] Question about parsing this line... [In reply to] Can't Post

Thanks a lot Kevin,
Your advice went a long way. I followed it and came up with a very good code. (At least it looks like it to me.) :)

Here's my finished code.


Code
#!/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;
use POSIX qw/strftime/;

my $DATE = strftime("%y%m%d", localtime(time));
my $FILE = $ARGV[0] || die "Usage: $0 <filename>\n";
open my $FH, '<', $FILE or die "Can't open '$FILE' $!";

while (<$FH>) {
if($_ =~ /^20/){
my ($DIFF, $CALL, $SEC1, $SEC2, $CL, $CLOG, $EL, $ELOG, $DIR, $C, $D);
my ($CHECK, $PA, $DC) = (quotewords('[\t,.]+', 0, $_))[2,14,16];

$DIR = "/home/voip/syslog/data/";

if($CHECK =~ /^Notice/){

$PA = substr($PA,13);
$DC = substr($DC,17);

if($PA =~ /^501/){
$CLOG = $DIR."ATT".$DATE.".txt";
} else{
$CLOG = $DIR."MCI".$DATE.".txt";
}

if($DC =~ /^1F/){
$C = "20";
$D = "23";
} else{
$C = "19";
$D = "22";
}

my ($DATETIME, $CID, $CT, $DT) = (quotewords('[\t,.]+', 0, $_))[0,10,$C,$D];

$CID = substr($CID,14);
$SEC1 = (substr("$CT",0,2) * 3600)+(substr("$CT",3,2) * 60)+substr("$CT",6,2);
$SEC2 = (substr("$DT",0,2) * 3600)+(substr("$DT",3,2) * 60)+substr("$DT",6,2);
$DIFF = $SEC2 - $SEC1;

print "Date: $DATETIME \n";
print "ConnectionID: $CID \n";
print "PeerAddress: $PA \n";
print "DisconnectCause: $DC \n";
print "ConnectTime: $CT \n";
print "DisconnectTime: $DT \n";
print "TotalTime: $DIFF \n";
print " \n\n";

$CALL = "$DATETIME,$CID,$PA,$CT,$DT,$DIFF\n";

open $CL, '>>', $CLOG or die "Can't open '$CLOG' $!";
print $CL $CALL;
close $CL;
}

if($CHECK =~ /^Error/){
$ELOG = $DIR."ErrorLog".$DATE.".txt";
open $EL, '>>', $ELOG or die "Can't open '$ELOG' $!";
print $EL $_;
close $EL;
}
}
}

close $FH;



Thanks again for everything guys.


Manikandan
New User

Feb 11, 2009, 10:52 PM

Post #6 of 6 (2288 views)
Re: [cuboidgraphix] Question about parsing this line... [In reply to] Can't Post

Thanks Kevin. Xactly what it was looking for :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives