Mike Miller | 11 Nov 2011 08:55
Picon

stripping extra lines from Alpine mbox files

When I use a program line MHonArc to make archives of email folders, I 
need to drop the first 13 lines of the mbox file whenever it is just that 
extra Alpine "FOLDER INTERNAL DATA" stuff.  So I drop the first 13 lines 
whenever the first line begins with "From MAILER-DAEMON ".

Here are two ways to do it that appear to be safe in the sense that they 
won't drop the initial lines unless they really are the Alpine internal 
data lines.  This creates a second file without the internal data lines:

perl -pe 'BEGIN{undef $/} ; s/\AFrom MAILER-DAEMON ([^\n]*\n){13}//s' infile > outfile

This changes the input file in place (which changes the files date stamp):

perl -pi -e 'BEGIN{undef $/} ; s/\AFrom MAILER-DAEMON ([^\n]*\n){13}//s' infile

With the latter method you could use globbing like so:

perl -pi -e 'BEGIN{undef $/} ; s/\AFrom MAILER-DAEMON ([^\n]*\n){13}//s' mail/*

That would remove the 13 internal data lines from every file in the folder 
'mail', but only if such lines existed in the file.  It would also change 
the date stamps on those files.

For in-place edits that retain a back-up copy of the original, add '.bak' 
after the 'i' option:

perl -pi.bak -e 'BEGIN{undef $/} ; s/\AFrom MAILER-DAEMON ([^\n]*\n){13}//s' mail/*

Then every original file in mail/ will still be there, but with .bak 
appended to the filename and the new edited versions will have the 
original names.

All of this works on Linux/UNIX and I assume it works on any other system 
that has perl installed and working.  But I now recall that perl in 
Windows under Cygwin requires the .bak -- even if you use -i it acts as if 
-i.bak had been used.

Mike

Gmane