Below code might be helpful...
Assuming you are passing log file as argument 1, and your values in log
file are space delimited,


use Data::Dumper;


open my $fd, "<", "$ARGV[0]" or die $!;


while(<$fd>) {


    next if (/^\s$/);


    my ($some_date, $some_time, $dom, $crt, $home)=split;


    if( exists $hash_val{$dom} ) {


        my $val=$hash_val{$dom};


        $val++;


        $hash_val{$dom}=$val;


    } else {


        $hash_val{$dom}=1;


    }


}


close $fd;


print Dumper \%hash_val;


RESULT will be -

$VAR1 = {
          'work:dom2' => 2,
          'test:dom2' => 1,
          'my:dom1' => 1,
          'work:dom1' => 1,
          'test:dom1' => 2,
          'my:dom2' => 2
        };


--
Cheers,
Shekar


On Sun, Apr 15, 2012 at 11:14 PM, Вячеслав Агапов <agapov.sl...@gmail.com>wrote:

> Hello all.
>
> I have a file with logs
> 2012-04-13 17:06:10,881 test:dom1 CRIT home
> 2012-04-13 17:06:10,882 work:dom1 CRIT home
> 2012-04-13 17:06:10,882 my:dom1 CRIT home
> 2012-04-13 17:06:10,881 test:dom2 CRIT home
> 2012-04-13 17:06:10,882 work:dom2 CRIT home
> 2012-04-13 17:06:10,882 my:dom2 CRIT home
> 2012-04-13 17:06:10,881 test:dom1 CRIT home
> 2012-04-13 17:06:10,882 work:dom2 CRIT home
> 2012-04-13 17:06:10,882 my:dom2 CRIT home
>
> I need print result
> test:dom1 - count of CRIT(2)
> test:dom2 - count of CRIT(5)
> work:dom1 - count of CRIT(6)
>
>
> File in .gz format
>
> my code
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use diagnostics;
> use 5.010;
>
> use IO::Compress::Gzip;
> use IO::Uncompress::Gunzip;
>
> my $file = "log.gz";
> my $ungzip = new IO::Uncompress::Gunzip($file);
> @arr = grep /work/, <$ungzip>;
>
> foreach my $text (@arr) {
>        @arr1 = split / /,$text;
>        @sort = ($arr1[3],$arr1[4]);
>        say "$sort[0] => $sort[1]";
> }
>
> result
> test:dom1 => CRIT
> test:dom2 => CRIT
> work:dom1 => CRIT
> test:dom1 => CRIT
> test:dom2 => CRIT
> work:dom1 => CRIT
>
> But, I need count of CRIT.
>
> I try
>
> $hash{$_}++ for @sort;
> print "$_ => $hash{$_}\n" for sort keys %hash;
> }
>
> But this print
> test:dom1 => 7
> test:dom2 => 63
> test:dom1 => 8
> test:dom2 => 64
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to