有一大堆日誌,需要按設備統計一下各個設備中日誌記錄中返回碼的個數。
設備名字可以根據文件名字獲得, 日誌記錄是有一定格式的。第七個字段是 返回碼。
分別shell和perl實現了,Perl不熟,網上拼了個代碼。
use Text::ParseWords; use Data::Dumper; my $result; my %table; $argv_len = @ARGV; if($argv_len == 0) { print "it need filename/n"; exit(1); } $cmd="ls '".join("' '",@ARGV)."' |"; open(PIPE, $cmd); @filenames = <PIPE>; close(PIPE); foreach(@filenames){ analy($_); } sub trim { my $string = shift; $string =~ s/^/s+//; $string =~ s//s+$//; return $string; } sub analy { my $filename= trim(shift); $cmd="cat $filename |"; open(PIPEFILE,$cmd); @lines=<PIPEFILE>; close(PIPEFILE); my ($sn,$dev_id,$time) = split(/_/g,$filename); my $dev=$dev_id.'-'.substr($sn,-3); my %code; foreach(@lines) { my @line=quotewords(" ", 1, $_); $code{$line[7]}++; } $table{$dev} = [] unless exists $table{$dev}; push @{$table{$dev}}, /%code; } foreach my $dev(sort keys %table) { print "$dev:/n "; my @code = @{$table{$dev}}; foreach my $code(@code) { foreach my $key(keys(%{$code})) { $result{$key}+= $code->{$key}; } } print "keys /t/t values/n"; foreach my $value (keys(%result)) { print $value." /t/t".$result{$value}; print "./n"; } }
使用shell就比較簡單了,只有幾行:
#!/bin/sh while read line do echo $line grep HTTP /path/20110527/ -r | grep $line | awk '{a[$10]++} END {for(i in a) print i " "a[i]}' done < dev_list