procmail
[Top] [All Lists]

Re: reverse the order of messages in a mailbox?

1999-10-13 20:50:06
At 10:32 AM 10/13/99 -0500, David W. Tamkin wrote:
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

(why -r?)

This will probably run into difficulty long before a million messages,
probably on a plausible though hefty number, when the filename
expansions of ./-* exceed the shell's command arguments length limit.

One could replace the last line with something (untested) like:
  ls -1 | egrep '^-' | tee /tmp/$$ | xargs cat && \
    touch ./-- && cat /tmp/$$ | xargs rm -f ./-- && rm /tmp/$$

(I may have missed something  :-)

Cheers,
Stan