Marc Perkel wrote:
>
> I need a script to take an mbox file with many messages in it - pipe it
> into a script and end up with just the headers. Removing the body of the
> message.
>
> Like to use it in a pipe - mbox goes in - headers comes out.
>
> Anyone have something quick and dirty that can do that?
Sure. Here is a quick and dirty perl script.
<get_headers.pl>
#!/usr/bin/perl
use strict;
my ($_firstHeader,$_lastHeader,$_message) = (0,0,0);
while (<STDIN>) {
($_firstHeader,$_lastHeader) = (1,0) and $_message++ if /^From /;
($_firstHeader,$_lastHeader) = (0,1) if /^$/;
s/^From .*$/\nMESSAGE HEADERS FOR MESSAGE: $_message/;
print STDOUT if $_firstHeader && !$_lastHeader && !/^\s*$/;
}
</get_headers.pl>
get_headers.pl <mbox_file
OR, should you just want all the headers with no seperation.
<get_headers.pl>
#!/usr/bin/perl
use strict;
my ($_firstHeader,$_lastHeader) = (0,0);
while (<STDIN>) {
($_firstHeader,$_lastHeader) = (1,0) if /^From /;
($_firstHeader,$_lastHeader) = (0,1) if /^$/;
s/^From .*$//;
print STDOUT if $_firstHeader && !$_lastHeader && !/^\s*$/;
}
</get_headers.pl>
get_headers.pl <mbox_file
OR, as a perl oneliner (watch for line breaks)
<perl_1line>
perl -e 'my($f,$l)=(0,0);while(<STDIN>){($f,$l)=(1,0)if/^From /;($f,$l)=
(0,1)if/^$/;s/^From .*$//;print if$f&&!$l&&!/^\s*$/;}'<mbox_file
</perl_1line>
Hope this helps you out.
--
--EAL--
--