On 15/04/2012 18:44, Вячеслав Агапов 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

As well as the problems that John Krahn has written about, the results
you say you expect don't seem to correspond to the data you show.

Below is a program that does what I think you need, using regular
expressions instead of 'split'.

HTH,

Rob


use strict;
use warnings;

use IO::Uncompress::Gunzip;

my $file = 'log.gz';
my $ungzip = new IO::Uncompress::Gunzip($file);

my %count;

/(\S+)\s+CRIT/ and $count{$1}++ while <$ungzip>;

print "$_ => $count{$_}\n" for keys %count;

**OUTPUT**

work:dom2 => 2
test:dom2 => 1
my:dom1 => 1
work:dom1 => 1
test:dom1 => 2
my:dom2 => 2

--
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