#!/usr/bin/perl -w # # $Id: mb,v 1.3 2008/10/16 16:59:53 vogelke Exp $ # # Count messages in mbox-formatted mailbox. # Build regular expression from pieces to find "From " lines: # # From owner-freebsd-questions@freebsd.org Mon Oct 13 23:57:01 2008 # |<-- email address -------------->| ^ ^ |<-- time/yr ->| # | | # | +---- months # +---- days # HISTORY: # Original script used Elm "frm" command: # exec /usr/local/bin/frm -Sq ${1+"$@"} use strict; # ------------------------------------------------------------------- # PART 1: Basic email address # Use ?: to show we don't care about capturing $1, $2, ... my $email = qr{ [A-z0-9/\+\*=_\-\.]+ # alphanum, slash/*/plus/equal/dash/underline/dot [@][A-z0-9_\-]+ # atsign alphanum plus underline or dash (?:[.][A-z0-9_\-]+)+ # with one or more dotted names }xms; # ------------------------------------------------------------------- # PART 2: Berkeley "From " lines. my $months = qr{ (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) }xms; my $days = qr{ (?:Sun|Mon|Tue|Wed|Thu|Fri|Sat) }xms; # Time and year. my $ty = qr{ \d+\s\d\d:\d\d:\d\d\s\d\d\d\d }xms; # Complete regexp for Berkeley 'From ' line. # You have to specify whitespace here. my $from = qr{ ( ^From\s+ $email\s+ $days\s+ $months\s+ $ty$ ) }xms; # ------------------------------------------------------------------- # Check your mail. my $count = 0; push (@ARGV, $ENV{'MAIL'}) unless @ARGV; while (<>) { chomp; if (/^From /) { if (/$from/) { $count++; } # ... leaves room for debug here. } } print "$count messages\n"; exit(0);