Вячеслав Агапов wrote:
Hello all.
Hello,
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
So this is what your actual data looks like?
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>;
With 'strict' enabled your program would have ended here because of the
@arr variable.
At this point @arr will only contain lines that have the pattern 'work'
in them.
foreach my $text (@arr) {
@arr1 = split / /,$text;
@sort = ($arr1[3],$arr1[4]);
With 'strict' enabled your program would have ended here because of the
@arr1 and @sort variables.
say "$sort[0] => $sort[1]";
}
result
test:dom1 => CRIT
test:dom2 => CRIT
work:dom1 => CRIT
test:dom1 => CRIT
test:dom2 => CRIT
work:dom1 => CRIT
According to your data and program the result should have been:
CRIT => home
CRIT => home
CRIT => home
CRIT => home
CRIT => home
CRIT => home
And how did 'test:dom1' get into your output if you used grep to filter
out those lines?
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
Please try to explain more clearly and post actual code and data.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/