How many messages are in my mailboxes?

ziggy on 2005-01-07T14:47:29

I'm in the middle of a periodic email purge at the moment. I want to count how many messages I have scattered across my mailboxes. On the mac, where I'm using Mail.app:

cd ~/Library/Mail/Mailboxes
find . -name mbox | sed -e 's/^\(.*\)$/"\1"/' | xargs grep -c '^From ' | cut -f 2 -d :  | total
On the mailbox at my ISP, where I use mutt:
cd ~/Mail
find . -type f | xargs grep -c '^From ' | cut -f 2 -d : | total
Where total is:
#!/usr/local/bin/perl
$total += $_ while (<>);
print "$total\n";


Not Perl?

VSarkiss on 2005-01-07T15:59:31

Whenever I see a big text processing pipeline in my stuff I think, "I'll combine it into a single Perl script". The funny part is that the one Perl script you have in your note is the one case where I still fall back on using awk, as in:

awk 's += $1; END {print s}'
That silly script is burned into my neurons from pre-Perl days, and refuses to go away....

Re:Not Perl?

ziggy on 2005-01-07T16:36:54

I dunno. I've got for loops and command pipelines wired into my hindbrain. I'd rather type out a long command than write a perl script for something like this.

If I execute a long command often enough, I do write a perl script for it; writing a perl script for a command I execute once or infrequently feels like a premature optimization.

Perl+= AWK Re:Not Perl?

n1vux on 2005-01-07T19:12:04

The AWK oneliner still works in Perl.
perl -lane '$s += $F[1]; END{print $s}'
-a says load @F array with AWK fields. And even better, $F[-1] is the last item.

TMTOWTDI *grin*

Aristotle on 2005-01-08T08:13:10

Optimizing for hack value:

( sed 'a \ +' ; echo 0 ) | xargs expr

Optimizing for minimum overhead:

{ total=0 ; while read c ; do (( total += c )) ; done ; echo $total ; }