rmck wrote:
Hello,

Hello,

Im trying to sum up a column from my results. Help.
current output:
Date_Time, SRCIP, DSTIP, TOTALBYTES
01-01-2004 12:56:48, 192.168.1.1, 192.168.2.2, 2768
Sum Of Bytes = 2768
01-01-2004 12:56:48, 192.168.2.2, 192.168.1.1, 438
Sum Of Bytes = 876
01-02-2004 16:49:45, 192.168.3.3, 192.168.4.4, 9058
Sum Of Bytes = 27174
01-02-2004 16:49:45, 192.168.4.4, 192.168.3.3, 918
Sum Of Bytes = 3672
goal:
Date_Time, SRCIP, DSTIP, TOTALBYTES
01-01-2004 12:56:48, 192.168.1.1, 192.168.2.2, 2768
01-01-2004 12:56:48, 192.168.2.2, 192.168.1.1, 438
Sum Of Bytes = 3206
01-02-2004 16:49:45, 192.168.3.3, 192.168.4.4, 364
01-02-2004 16:49:45, 192.168.4.4, 192.168.3.3, 513
Sum Of Bytes = 877
Im stuck. Should I use a hash??

It looks like a hash should help.


Current Script:
#!/usr/bin/perl
use Socket;
use strict;
use POSIX 'strftime';
use warnings;
my $time = strftime "%y%m%d%H", localtime;
my $count = "0";

Why are you assigning a string to $count when it will be used in a numerical context? Although perl will do the right thing and treat it as the number zero, someone else reading your code might not understand it.

my $count = 0;


# open the file
open(LOG,"@ARGV") or die "Unable to open LOG:$!\n";

That is the same as saying:

open(LOG,join($",@ARGV)) or die "Unable to open LOG:$!\n";

Which will *ONLY* work if there is *ONE* file name on the command line.


open(LOG,$ARGV[0]) or die "Unable to open $ARGV[0]:$!\n";


print "Date_Time, SRCIP, DSTIP, TOTALBYTES \n";

# read it in one record at a time
while (<LOG>) {
my ($logdate,$srcip,$dstip,$totalbytes) = split(/\t/,$_);
my ($date,$time )= split(/\s/,$logdate);
my @hour = split(/:/,$time);
next if $_ =~ /^\D/;

You should run this test first so you don't try to split an invalid line.


if ($hour[0] >= 6 and $hour[0] < 22){
print  "$logdate,$srcip,$dstip,$totalbytes";
$count++;
my $sum = $count * $totalbytes;
print "Sum of Bytes = $sum\n";
                              }
                                       }
# close the file
close(LOG);


Since you didn't provide an example of the input data I can't test this but
this should be close to what you want:


#!/usr/bin/perl use warnings; use strict;

use Socket;
use POSIX 'strftime';
my $time = strftime '%y%m%d%H', localtime;


print "Date_Time, SRCIP, DSTIP, TOTALBYTES\n";

my %data;
# read it in one record at a time
while ( <> ) {
    next unless /^(\d\d-\d\d-\d{4}
                   [ ]
                  (\d\d):\d\d:\d\d)
                   \t
                  (\d{1,3}(?:\.\d{1,3}){3})
                   \t
                  (\d{1,3}(?:\.\d{1,3}){3})
                   \t
                  (\d+)
                   \n
                /x;
    next if $2 < 6 or $2 >= 22;
    my ( $date, $srcip, $dstip, $bytes ) = ( $1, $3, $4, $5 );

if ( exists $data{ $date } ) {
push @{ $data{ $date }{ lines } }, "$date,$srcip,$dstip,$bytes\n";
$data{ $date }{ total } += $bytes;
}
else {
print @{ $data{ $date }{ lines } }, "Sum of Bytes = $data{$date}{total}\n";
$data{ $date }{ lines } = [ "$date,$srcip,$dstip,$bytes\n" ];
$data{ $date }{ total } = $bytes;
}
}




John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to