#!/usr/bin/perl $/="\nFrom "; use Email::Simple; use Getopt::Std; my %opts; getopt("sftT", \%opts); my $term=shift @ARGV; MAIN: while (<>) { chomp; # Quick scan, to weed out common non-matches. next if $opts{s} and !/Subject:.*$opts{s}.*$/m; next if $opts{f} and !/From:.*$opts{f}/m; next if $opts{T} and !/(To|Cc):.*$opts{T}/m; next if $opts{t} and !/To:.*$opts{t}/m; next if $term and not m/$term/; # Detailed scan. $_="From $_\n"; my $item = new Email::Simple ($_); next if $opts{s} and $item->header(q/Subject/) !~ /$opts{s}/; next if $opts{f} and $item->header(q/From/) !~ /$opts{f}/; next if $opts{t} and $item->header(q/To/) !~ /$opts{t}/; next if $opts{T} and $item->header(q/To/) !~ /$opts{T}/ and $item->header(q/Cc/) !~ /$opts{T}/; next if $term and $item->body !~ /$term/smi; print; }