Wednesday, 27 August 2008

perl multi-line sort and uniq

perl -00 is so handy sometimes.
I keep on having to do awkward multi-line grep/sedding.

Here we grep for two multi-line fields matching "status:" and "Thread stack".
print "$_\n" if ( /Thread stack(/ || /status:/ );
We append them together.
$_ =~ s/(status:.*)\n\n/\1/m;
Later we use a hash to count uniq stack dumps.
perl -00wne 'BEGIN { my %counts } $counts{$_}++; END { foreach my $key (keys(%counts)) {print "count $counts{$key} of $key\n";} }'

# summarize cores
# sort and count by core stack seen (using perl)
# we look for core.appcore*.gz filerotated (solaris mdb) cores

APPCOREFILES=$(ls core.appcore*.gz)
echo > core.appcore_summary.txt
for F in $APPCOREFILES ; do
#gzcat $F |gegrep -A5 "^status:|Thread stack";
gzcat $F |sed "s/^ *$//" | perl -00 -ne "\$_ =~ s/(status:.*)\\n\\n/\\1/m; print \"\$_\\n\" if ( /Thread stack\(/ || /status:/ ); " >> core.appcore_summary.txt

uname -a |tee core_report.txt
ls -al core.appcore*.gz |tee -a core_report.txt
perl -00wne 'BEGIN { my %counts } $counts{$_}++; END { foreach my $key (keys(%counts)) {print "count $counts{$key} of $key\n";} }' core.appcore_summary.txt |tee -a core_report.txt