Blast output - program to count the number of hits
This script, named blast2table.pl is from the code accompanying the Blast book by Korf &amp; Yandell (there is a 3rd author whom I haven't met).

#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use vars qw($opt_p $opt_b $opt_e $opt_m $opt_n);
    getopts('p:b:e:m:n:');
    my $PERCENT = $opt_p ? $opt_p : 0;
my $BITS    = $opt_b ? $opt_b : 0;
    my $EXPECT  = $opt_e ? $opt_e : 1e30;
my $START   = $opt_m ? $opt_m : 0;
    my $END     = $opt_n ? $opt_n : 1e30;

my ($Query, $Sbjct);
my $HSP = "";
    while (&lt;&gt;) {
        if    (/^Query=\s+(\S+)/) {outputHSP(); $Query = $1}
        elsif (/^&gt;(\S+)/)         {outputHSP(); $Sbjct = $1}
        elsif (/^ Score = /) {
            outputHSP();
            my @stat = ($_);
            while (&lt;&gt;) {
                last unless /\S/;
                push @stat, $_
            }
            my $stats = join("", @stat);
            my ($bits) = $stats =~ /(\d\S+) bits/;
            my ($expect) = $stats =~ /Expect\S* = ([\d\.\+\-e]+)/;
       $expect = "1$expect" if $expect =~ /^e/;
            my ($match, $total, $percent) = $stats =~ /Identities = (\d+)\/(\d+) \((\d+)%\)/;
            my $mismatch = $total - $match;

        $HSP = {bits =&gt; $bits, 
                    expect =&gt; $expect, 
                    mismatch =&gt; $mismatch, 
                    percent =&gt; $percent, 
                    q_begin =&gt; 0, 
                    q_end =&gt; 0, 
                    q_align =&gt; 0, 
                    s_begin =&gt; 0, 
                    s_end =&gt; 0, 
                    s_align =&gt; ""};
        }
        elsif (/^Query:\s+(\d+)\s+(\S+)\s+(\d+)/) {
            $HSP-&gt;{q_begin}  = $1 unless $HSP-&gt;{q_begin};
            $HSP-&gt;{q_end}    = $3;
            $HSP-&gt;{q_align} .= $2;
        }
        elsif (/^Sbjct:\s+(\d+)\s+(\S+)\s+(\d+)/) {
            $HSP-&gt;{s_begin}  = $1 unless $HSP-&gt;{s_begin};
            $HSP-&gt;{s_end}    = $3;
            $HSP-&gt;{s_align} .= $2;
    }
}
outputHSP();

sub outputHSP {
    return unless $HSP;
        return if $HSP-&gt;{percent}  &lt; $PERCENT;
        return if $HSP-&gt;{bits}     &lt; $BITS;
        return if $HSP-&gt;{expect}   &gt; $EXPECT;
        return if ($HSP-&gt;{q_begin} &lt; $START or $HSP-&gt;{q_end} &lt; $START);
        return if ($HSP-&gt;{q_begin} &gt; $END   or $HSP-&gt;{q_end} &gt; $END);
        foreach my $field ('percent', 'q_align', 'mismatch', 's_align', 'q_begin', 'q_end', 's_begin', 's_end', 'expect', 'bits'){
        print "$field not defined\n" if not defined $HSP-&gt;{$field};
        }
        print join("\t", $Query, $Sbjct, $HSP-&gt;{percent}, length($HSP-&gt;{q_align}), $HSP-&gt;{mismatch}, countGaps($HSP-&gt;{q_align}) + countGaps($HSP-&gt;{s_align}), $HSP-&gt;{q_begin}, $HSP-&gt;{q_end}, $HSP-&gt;{s_begin}, $HSP-&gt;{s_end}, $HSP-&gt;{expect}, $HSP-&gt;{bits}), "\n";
    $HSP = "";
}

sub countGaps {
    my ($string) = @_;
        my $count = 0;
    while ($string =~ /\-+/g) {$count++}
        return $count;
}