Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!nsc!voder!berlioz.nsc.com!nelson From: nelson@berlioz.nsc.com (Taed Nelson) Newsgroups: comp.unix.questions Subject: Re: Sorting mail files chronologically on Unix Message-ID: <1991May2.004556.4234@berlioz.nsc.com> Date: 2 May 91 00:45:56 GMT References: <26667@adm.brl.mil> <1991Apr29.204812.3476@mrspoc.Transact.COM> <22620@yunexus.YorkU.CA> Sender: news@berlioz.nsc.com Organization: PLD Software Group, National Semiconductor Co., Santa Clara, CA. Lines: 187 >>If someone actually goes to the trouble to develop such a program, I'm >>sure that they'd find a large number of people who'd be interested in it. Some kind soul sent me this script a while back when I asked the same question. If the name is not in the comments, then I don't know who wrote it. It is included below. It is followed by an AWK script which I wrote (borrowing from the aformentioned program) to delete any pieces of mail which have a Subject which starts with the character `@'. The reason I have this is because I save all of my outgoing mail in a record, but sometimes I send out junk that I don't want wasting space in my record. By putting an `@', I allow that mail to be deleted when I run the script under AWK. SORT MAIL: ------------------------------------------------------------------------------ #!/bin/sh # # sort mail in a folder chronologically # # From unido!uunet!amdahl!rtech!llama!daveb Thu Jan 28 09:00:34 1988 # Received: by fb10vax.sbsvax.uucp; Thu, 28 Jan 88 09:00:32 +0100 (MET) # Date: Wed, 27 Jan 88 16:54:52 pst # From: unido!uunet!amdahl!rtech!llama!daveb (Dave Brower) 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 '') echo -n "Name the mail folder to sort: " read old ;; 1) old=$1 ;; *) echo "Usage: $myname old" exit ;; esac if [ ! -f $old ] then echo "$old: no such file" exit elif [ ! -w $old ] then echo "$old: no write access" exit fi 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 ------------------------------------------------------------------------------ KILL MAIL: ------------------------------------------------------------------------------ # The AWK script which deletes all mail on stdin with the first subject # character as `@'. # Written April 1991 by Taed Nelson (nelson@desktop.nsc.com) # Place the message or the packet record in the appropriate file. function sortMessage() { if ((saveMode != KILL) && (saveMode != NONE)) { # Don't use "i in messageStore" since it doesn't keep the order # Also, get rid of trailing blanks since I hate them for (i=1; i <= (numLines - trailingBlanks); i++) { print messageStore[i]; delete messageStore[i]; } # Make sure there's a newline between the records printf ("\n"); } } BEGIN { numLines = 0; NONE = 0; KILL = 1; MESSAGE = 2; UNKNOWN = 3; # So that we don't have any output if there are only kill messages saveMode = NONE; } # The "From" line contains the date we received the message. # It better be the first line, or it's going to get confused. ($1 == "From") && (NF == 7) { sortMessage(); saveMode = UNKNOWN; numLines = 0; ++messageNum; } # If we know we're collecting a kill or the whitespace between messages, # we can skip to the next message. (saveMode == KILL) || (saveMode == NONE) { next; } # Always collect the text of the messages. { messageStore[++numLines] = $0; if ((saveMode == UNKNOWN) && ($1 ~ /^Subject:$/)) { if ($2 ~ /^\@/) { saveMode = KILL; } else { saveMode = MESSAGE; } } if ($0 ~ /^$/) { trailingBlanks++; } else { trailingBlanks= 0; } } # Don't forget to sort the last message! END { sortMessage(); }