I don't think that you want to print this out after every loop. I think you want to print out after every day. Maybe try this. Also I think the way you are looping through the lines is a little messed up. The while is looping through the lines, and the until really isn't doing anything.
Try something like this(untested):
#!/usr/bin/perl -w -d
use strict;
my $date;
my $line;
my ($tot_ip_cnt,$tot_icmp_cnt,$tot_tcp_cnt,$tot_udp_cnt,$tot_events,$t
+ot_drops) = (0,0,0,0,0,0);
open (INFILE, "< hbeat-app.log.samp") or die ("Error Opening File $!\n
+");
open (OUTFILE, ">> HB_APP_totals.log.CSV") or die ("Error Opening File
+ $!\n");
print OUTFILE "date,ip_cnt,icmp_cnt,tcp_cnt,udp_cnt,events,drops \n";
while ( my $line = <INFILE>) {
if ($line =~ m/^(\d{4})(\w{3})(\d+).+/) {
print OUTFILE "$date,$tot_ip_cnt,$tot_icmp_cnt,$tot_tcp_cnt,$t
+ot_udp_cnt,$tot_events,$tot_drops\n" if(length($date) > 0);
$date = join ' ',($2,$3,$1);
} else {
$line =~ m/^EVENT1\:\s+\[\w+\]\s+\(IP\=(\d+)\,ICMP\=(\d+)\,TCP\=(
+\d+)\,UDP\=(\d+)\,EVENTS\=(\d+)\,DROP\=(\d+)\,.+/;
$tot_ip_cnt += $1;
$tot_icmp_cnt += $2;
$tot_tcp_cnt += $3;
$tot_udp_cnt += $4;
$tot_events += $5;
$tot_drops += $6;
}
}
print OUTFILE "$date,$tot_ip_cnt,$tot_icmp_cnt,$tot_tcp_cnt,$tot_udp_c
+nt,$tot_events,$tot_drops\n";
close OUTFILE;
close INFILE;
Update: Added print after loop to print out last day.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.