procmail
[Top] [All Lists]

Re: reverse the order of messages in a mailbox?

1999-10-13 08:37:19
Robert Borwn asked,

| Does anyone have a script which would reverse the order of messages in a
| mailbox?  I could think of some solutions making use of formail, $FILENO,
| and sort, but if someone has already done this, why re-invent the wheel?

We can avoid calling sort if we make the shell do it.  To avoid mkdir and
rmdir, let's assume further that you have more sense than to use filenames
that begin with hyphens, and to simplify matters let's assume that you have
fewer than one million messages in the folder.  The leading 0 in the initial
value of FILENO is critical so that the values will have a constant field-
width:

 #!/bin/sh

 FILENO=-01000000 formail -s \
     procmail -p DEFAULT=${PWD-`pwd`}/'$FILENO' /dev/null # from stdin
 cat ./-* && rm -r ./-* # to stdout