procmail
[Top] [All Lists]

Re: Counting messages in a mailbox

1996-12-08 05:16:07

I'd like to be able to count the number of messages in a mailbox.
The simple-minded approaches, such as:
      egrep -i '^From ' mailbox | wc -l
      egrep -i '^Subject:' mailbox | wc -l
don't work for fairly obvious reasons.

You can speed this up a little:

    grep -c '^From ' mailbox

The only false matches will be for sentences which start with 'From '.

You could also try awk:

  awk '/^From / {if (!header) { header=1; msgs++ }}
       /^ *$/   {header=0}
       END      {print msgs,"\n"}' mailbox

or Perl:

  perl -ne 'if (/^From /&&\!$header) { $header=1; $msgs++; }
            elsif (/^\s*$/) { $header=0; }
            if (eof) { print "$msgs\n"; }' mailbox

Perl seemed to be faster than awk; the "grep" was fastest, but is not
always accurate.

This seems to work:
      formail -s formail -X 'From ' < mailbox | wc -l
but due to the number of processes, it's *very* slow.

You've got one too many formail's:

    formail -s echo x < mailbox | wc -l
or:
    formail -s echo -c x < mailbox | wc -c

There's no need to extract anything out of the message, just being able
to run any program once on behalf of each message is sufficient.

___________________________________________________________
Alan Stebbens <aks(_at_)sgi(_dot_)com>      http://reality.sgi.com/aks

<Prev in Thread] Current Thread [Next in Thread>