in reply to Re: Re: Multiple Sorts
in thread Multiple Sorts
while (<DATA>) { # skip blank and commented lines next if /^\s*#/; next if /^\s*$/; # We'll look for lines that describe a report: if (s/^\s*report:\s+//i) { # line is a report description chomp; @report{qw(sort list)} = split; # set defaults $report{sort} ||= 'source'; # $report{acl} ||= 'all'; } else { my ($log_source,$time,$source_ip,$source_port,$dest_ip,$dest_port, $repeats,$acl_number,$log_number) = (split/,/); $list{$_}={site => $log_source,time =>$time, sip=>$source_ip, sport =>$source_port, dip => $dest_ip, dport =>$dest_port, hits =>$repeats, acl =>$acl_number, lnum =>$log_number}; } } #close fh; if ( $report{acl} eq 'all' ) { for( qw(site-a time-a sport-d hits-a) ){ list_by_sort($_, \%list) if $report{sort} eq 'sort'; print "\n" } } else { list_by_sort($report{list}, \%list) if $report{sort} eq 'sor +t'; } sub ST(&@){ my $metric=shift; map {$_->[0]} sort {$a->[1] cmp $b->[1]} map {[$_,&{$metric}]} @_ } sub list_by_sort{ my @fields = split/,/,shift; my $list = shift; #input from the report line of the data my $date = localtime; $date =~ s/ /-/g; my $report_file_name = "$date.SORT"; ###stores report data in file open OF, ">>$report_file_name" or die "Can't Open $report_file +_name: $!"; print OF " REQUEST FOR SORTING\n\n"; print OF "FILE WAS GENERATED ON: $date \n"; #know I don't know how to compare the fields and do the sorts #the -a is for asc. and -d for descending print OF ST{ my $key=$_; my %h = %{$list->{$key}}; join"\0",map{ my($k,$d)=split/-/; $d eq "d"?~$h{$k}:$h{$k}; }@fields; } keys %$list; } # __DATA__ report: sort site-a,time-a,sport-d,hits-a Monmouth,2000-05-2000:00:09-04,192.35.75.69,138,192.100.255,66,2,101,1 +234 Jackson,2000-04-2100:00:10-05,192.35.12.03,144,192.67.29,134,8,101,148 +7 Meade,2001-01-0500:00:11-04,213.132.32,175,184.57.62.35,151,12,101,153 +2
|
|---|