I have been using this script for years at work to summarize log files.
#!/usr/bin/perl
$logf = '/var/log/messages.log';
@logf=( `cat $logf` );
foreach $line ( @logf ) {
$line=~s/\d+/#/g;
$count{$line}++;
}
@alpha=sort @logf;
$prev = 'null';
@uniq = grep($_ ne $prev && ($prev = $_), @alpha);
foreach $line (@uniq) {
print "$count{$line}: ";
print "$line";
}
I have wanted to rewrite it in Python but I do not fully understand certain portions of it, such as:
@alpha=sort @logf;
$prev = 'null';
@uniq = grep($_ ne $prev && ($prev = $_), @alpha);
Does anyone know of a Python module that would negate the need to rewrite this? I haven’t had any luck find something similar. Thanks in advance!
would be equivalent to
if
logfwere a list of lines.However, since you are counting the freqency of lines,
you could use a collections.Counter to both count the lines and collect the unique lines (as keys) (thus removing the need to compute
uniqat all):