[comp.unix.questions] sort mbox by date?

daveb@rtech.UUCP (Dave Brower) (01/28/88)

   Date: Tue, 26 Jan 88 13:56:23 pst
   From: sun!teknowledge-vaxc.ARPA!mkhaw (Mike Khaw)

   > I am looking for something that will read an mbox format file, and
   > produce a new file that contains all the messages within sorted by
   > date.  Does anyone have a good way of doing this?

   I have a sh+awk script combo that does it in a brute force sorta
   way...

It did nearly what I wanted, though I've hacked it a bit more.  Here it
is for others, since it is _so_ brute force I don't think I want to put
it into comp.sources...

Thanks, all!

-dB

---------------- cut here ----------------

cat > sortmbox << "END_OF_SHAR"
#!/bin/sh
# sort mail in a folder chronologically
#
# Authors:  mkhaw@teknowledge-vaxc.arpa, daveb@rtech.uucp
#
# This version for BSD; SV milage may vary (see echo -n below).

new=sorted.$$
myname=`basename $0`
case $MAILSORT in	# assume MAILSORT is an env. variable
-v)	RMFLAG='-i' ;;
*)	RMFLAG='-f'
	trap 'rm -f $new ; exit' 0 1 2 3 5 9 ;;
esac

case $# in
0)	echo -n "Name the mail folder to sort: "
	read old ;;
1)	old=$1	;;
*)	echo "Usage: $myname old"; exit	;;
esac

[ ! -f $old ] && echo "$old: no such file" && exit
[ ! -w $old ] && echo "$old: no write access" && exit

awk 'BEGIN {
	month["Jan"] = 1
	month["Feb"] = 2
	month["Mar"] = 3
	month["Apr"] = 4
	month["May"] = 5
	month["Jun"] = 6
	month["Jul"] = 7
	month["Aug"] = 8
	month["Sep"] = 9
	month["Oct"] = 10
	month["Nov"] = 11
	month["Dec"] = 12
}
/^From / {
    ++msg
    if( NF == 7)
	hold = sprintf("%s:%02d:%02d:%s %d", $7, month[$4], $5, $6, msg)
    if( getline && NF == 7 && $1 == "Date:")
        hold = sprintf( "19%s:%02d:%02d:%s %d", $5, month[$4], $3, $6, msg)
    print hold
} ' $old | sort -n |  sed "s/.* \(.*\)/s \1 $new/" | mail -f $old > /dev/null

if [ $? = 0 ]
then
	mv $RMFLAG $new $old
	echo "$old is now sorted"
else
	echo "Problems sorting $old, left untouched"
fi
exit 0

END_OF_SHAR
chmod +x sortmbox
exit 0
-- 
"If it was easy, we'd hire someone cheaper than you to do it."
{amdahl, cbosgd, mtxinu, ptsfa, sun}!rtech!daveb daveb@rtech.uucp