I have to do a group by and sum two columns, I am able to do only for two columns. For the below code, I got the help from Monks only. I need another help to group by and sum two columns. I have grouped by using col2, and sum col6 and col7
#!/usr/bin/perl #use strict; #use warnings; use feature 'say'; #my %hash; print "Please input the Service Name(V/M/S/ALL):"; my $sinput = <STDIN>; chomp $sinput; print "Please input On/Off/ALL:"; my $chcat= <STDIN>; chomp $chcat; print "Start Date:"; chomp($sdate=<>); print "End Date:"; chomp($edate=<>); $file = '/var/tmp/CA_scripts/1_onnet_offnet/Onnet_offnet_repository.cs +v'; open (my $fh, '>>', 'C_n_r_'.$sinput.'.csv'); unless(open FILE, $file) { die "\nUnable to open the file\n"; } while (my $line = <FILE>) { my ($col1,$col2,$col3,$col4,$col5,$col6,$col7,$col8) = split /,/, +$line; #my $col21 = $class; if ($sdate >= $col3 && $edate <= $col3 && $sinput eq $col5 && $chcat e +q "On" && ($col6 =~ /^\Q97517\E/ || $col6 =~ /^\Q97516\E/)) { $hash{"$col2"} += $col8; #$hash{"$col2"} += $col6; } elsif ($sdate >= $col3 && $edate <= $col3 && $sinput eq $col5 && $chca +t eq "Off" && $col6 =~ /^\Q97577\E/) { $hash{"$col2"} += $col8; } elsif ($sdate >= $col3 && $edate <= $col3 && $sinput eq "ALL" && $chc +at eq "ALL") { $hash{"$col2"} += $col8; } } foreach my $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) { printf $fh "%s %1f\n", $key, $hash{$key};
In reply to group by and sum for two columns by gowthamvels
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |