I tried the suggestion below, and it appears to have worked, thanks. I had to add a "print $sum !=" to inside and outside the while loop. Im not sure why it is working this way?? Also Im getting an error "Use of uninitialized value in string ne at ./clean1.pl line 28, <LOG> line 1." ?? If anyone can explain I would appericate:
#!/usr/bin/perl use Socket; use strict; use POSIX 'strftime'; use warnings; my $line = $ARGV[0]; my $time = strftime "%y%m%d%H", localtime; my $sum = 0; my $prev_date; # open the file open(LOG,"$line") or die "Unable to open LOG:$!\n"; print "Date_Time, SRCIP, DSTIP, TOTALBYTES \n"; # read it in one record at a time #while (<LOG>) { while ($line = <LOG>) { next if $line =~ /^\D/; my ($logdate,$srcip,$dstip,$totalbytes) = split(/\t/,$line); my ($date,$time)= split(/\s/,$logdate); my @hour = split(/:/,$time); my $current_date = $date; if ($hour[0] >= 6 and $hour[0] < 22){ if ($prev_date ne $current_date) { ##LINE 28## #print "Total: $sum\n"; # display before clearing, Prints a Total: 0 on first line if ($sum != 0) { print "Total: $sum\n"; } $sum = 0; } $sum += $totalbytes; $prev_date = $current_date; print "$logdate,$srcip,$dstip,$totalbytes"; } # End Of While: } if ($sum != 0) { print "Total: $sum\n"; } # close the file close(LOG); output: Date_Time, SRCIP, DSTIP, TOTALBYTES Use of uninitialized value in string ne at ./clean1.pl line 28, <LOG> line 1. 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 Total: 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 Total: 877 Thank You. Rob -----Original Message----- From: Wiggins d Anconia [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 5:32 PM To: rmck; [EMAIL PROTECTED] Subject: Re: sum a column > 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?? > Hash shouldn't be necessary unless they are not ordered. > 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"; > No need to quote integers during assignment. > # open the file > open(LOG,"@ARGV") or die "Unable to open LOG:$!\n"; > Why are you opening @ARGV, doesn't seem like this would work, that or Perl is doing something under the hood that I don't expect, but very little surprises me :-). > 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); You can capture the above to get just the hour if you want to, with, my ($hour) = split(/:/, $time); > > next if $_ =~ /^\D/; Why do this down here, it seems it would be more efficient to catch it as early as possible. > > if ($hour[0] >= 6 and $hour[0] < 22){ > print "$logdate,$srcip,$dstip,$totalbytes"; > $count++; > my $sum = $count * $totalbytes; This is likely your problem. Are you really trying to sum the # of bytes? If so why are you multiplying it times the $count, which is seemingly the line of the file that you are currently processing. Also, $sum will have to be scoped ouside the loop otherwise it will be reset for each line of input that you process, you really need to keep it for each iteration and then only clear it when the hour/date you are currently on is not the same as the previous hour/date. > print "Sum of Bytes = $sum\n"; > } > } > # close the file > close(LOG); > > > Thanks for any input...... > > Rob > So in pseudo code it will look something like: my $sum = 0; my $prev_date; while (my $line = <LOG>) { . . . my $current_date = ....; if ($prev_date ne $current_date) { print "Total: $sum\n"; # display before clearing $sum = 0; } $sum += $totalbytes; $prev_date = $current_date; } # don't forget the last one, aka the while loop has stopped # so we need one last "date change" if ($sum != 0) { print "Total: $sum\n"; } http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>