MH & nmh: Email for Users & Programmers

May, 2006

Sorting Messages: sortm

After a lot of refiling and rearranging, the messages in your folders won't be in order by date anymore. If you want to sort a folder, use the sortm command.

MH now has two versions of sortm:

Here's a short example of a folder before and after sorting:
    % scan +money
      3  07/14 yourfriend       Would you loan me $1,000,000?<<Dear
      6  06/28 To:mom@home      Your rich son<<Dear Mom, I just won
      7+ 06/28 vannaw@wheel.for A prize!!!<<Hi!!!!!  Guess what????
    % sortm
    % scan
      3+ 06/28 vannaw@wheel.for A prize!!!<<Hi!!!!!  Guess what????
      6  06/28 To:mom@home      Your rich son<<Dear Mom, I just won
      7  07/14 yourfriend       Would you loan me $1,000,000?<<Dear
    
In that example, sortm swapped messages 6 and 7 because message 7 was sent earlier the same day. It preserved the current message. The version of sortm in MH 6.6 and before would also have renumbered the messages 1, 2, and 3.

Here's the same folder sorted by subject with the -textfield switch and also with the -verbose switch, to show you how that looks:

    % scan +money
      3  07/14 yourfriend       Would you loan me $1,000,000?<<Dear
      6  06/28 To:mom@home      Your rich son<<Dear Mom, I just won
      7+ 06/28 vannaw@wheel.for A prize!!!<<Hi!!!!!  Guess what????
    % sortm -limit 0 -verbose -textfield subject
    sorting by subject-major date-minor
    renaming message chain from 7 to 3
    message 6 becomes message 7
    message 3 becomes message 6
    % scan
      3  06/28 vannaw@wheel.for A prize!!!<<Hi!!!!! Guess what????
      6  07/14 yourfriend       Would you loan me $1,000,000?<<Dear
      7  06/28 To:mom@home      Your rich son<<Dear Mom, I just won
    
Section Date Limit explains -limit.

If you have a lot of folders and you want to sort all of them, you can type one of the loops in the Example below on your terminal. They get a list of folders from the folder -fast -recurse command (if you don't have any subfolders, leaving out the -r will make this faster). Then they display the name of the folder on your terminal and sort with sortm.

Two notes for people who've never entered a loop like this:

  1. Be sure to use backquotes (`), not single quotes (').
  2. Don't type the question mark (?) or the right angle bracket (>) characters -- they are "secondary prompts" from the shells, and you should see them as soon as you type the first line and press RETURN.
The next example compares the C shell with the Bourne and Korn shell syntax.

Example: Using shell loops to sort all folders

C shell:

    % foreach f (`folders -f -r`)
    ? echo sorting folder $f
    ? sortm +$f
    ? end
    sorting folder apple
        ...
    sorting folder zoo/zebra
    %
    
Bourne and Korn shells:
    $ for f in `folders -f -r`
    > do echo sorting folder $f
    > sortm +$f
    > done
    sorting folder apple
        ...
    sorting folder zoo/zebra
    $
    
If you haven't seen shell variables and loops, look at the Chapter Introduction to UNIX Programming with MH. By the way, sorting all your folders can keep your computer busy for a while. If there are many other users, you'll make them happier by using the command nice sortm instead of plain sortm; your sortm commands will run at a lower priority. See your online manual page for nice(1) or your shell.

Sort by Any Field

In MH 6.7 and later, sortm is more flexible than older versions. The -textfield and -datefield options will sort on any text or special date field in the message header. In MH 6.6 and before, sortm doesn't have these switches.

For instance, the command sortm -textfield subject would sort messages by their subjects. When you sort by subject, sortm will ignore any Re: at the start of a subject. That groups replies with the original message.

Note that sortm sorts by the entire field (except the field name, like From:). But scan may show only part of a field, like the person's real name in a From: field -- and make you think that the sort didn't work.

Date Limit

When you sort messages on a text field with the -textfield option, you can also group messages by the dates they were sent. In MH 6.7 and above, the -limit option does this. These switches aren't in previous MH versions.

The easiest way to learn about -limit is with some examples. To start, here's a folder with messages from two different users. First, I'll use the scan.time format file to show the time that each message was sent. A lot of the messages were sent around the start of July, and a couple were sent later:

    % scan -form scan.time
       1  07/02 17:34CDT Al Bok             Results are in, and
       2  07/02 13:43CDT Dave Lamphear      Status report<<Hi,
       4  07/03 09:43CDT Al Bok             More results<<Thing
       5  07/03 11:09CDT Dave Lamphear      Have a great Fourth
       7  07/05 08:11CDT Al Bok             Finished<<That's it
       9  07/12 13:13CDT Al Bok             The next project...
      11  07/13 11:58CDT Dave Lamphear      Re: The next projec
    

Group Messages Sent Within 1 Day of Each Other: -limit 1

Let's sort the folder by the From: field. If you use -limit 1 on the command line (that's a digit 1, not the letter "l"), it tells sortm to group messages sent within one day of each other, with the same From: field. Now, sort and scan:

    % sortm -textfield from -limit 1
    % scan -form scan.time
       1  07/02 13:43CDT Dave Lamphear      Status report<<Hi,
       2  07/03 11:09CDT Dave Lamphear      Have a great Fourth
       4  07/02 17:34CDT Al Bok             Results are in, and
       5  07/03 09:43CDT Al Bok             More results<<Thing
       7  07/05 08:11CDT Al Bok             Finished<<That's it
       9  07/12 13:13CDT Al Bok             The next project...
      11  07/13 11:58CDT Dave Lamphear      Re: The next project
    
That sort put the two messages from Dave together, first in the list, because the earliest message in the folder was from Dave. (Look at the time-of-day field.) Why didn't it put the third message from Dave (sent 07/13) together with the first two of his (sent 07/02 and 07/03)? That's because the third message was sent more than one day after the other two -- and the -limit 1 told sortm to do that.

The first two messages from Al were sent within a day of each other, so they come out next to each other. Two more messages of his come next, strictly in date order.

Group All Messages with Same Field: -nolimit

Let's resort the folder, this time with -nolimit.

NOTE: The -nolimit switch is the default for sortm. You don't have to type it.

The -nolimit switch means that messages with the same field (here, the From: field) are grouped together, no matter how many days difference between the times they were sent:

    % sortm -textfield from -nolimit
    % scan -form scan.time
       1  07/02 13:43CDT Dave Lamphear      Status report<<Hi,
       2  07/03 11:09CDT Dave Lamphear      Have a great Fourth
       4  07/13 11:58CDT Dave Lamphear      Re: The next projec
       5  07/02 17:34CDT Al Bok             Results are in, and
       7  07/03 09:43CDT Al Bok             More results<<Thing
       9  07/05 08:11CDT Al Bok             Finished<<That's it
      11  07/12 13:13CDT Al Bok             The next project...
    
In this example, all of Dave's messages are grouped together. They came before Al's messages because Dave sent his first message before Al.

Group Messages by Field Then Date: -limit 0

Use -limit 0 (that's the digit 0, not the letter "O") to sort by the field first, then by the date. If you know sorting terminology, this sort is "field-major, date-minor":

    % sortm -textfield from -limit 0
    % scan -form scan.time
       1  07/02 17:34CDT Al Bok             Results are in, and
       2  07/03 09:43CDT Al Bok             More results<<Thing
       4  07/05 08:11CDT Al Bok             Finished<<That's it
       5  07/12 13:13CDT Al Bok             The next project...
       7  07/02 13:43CDT Dave Lamphear      Status report<<Hi,
       9  07/03 11:09CDT Dave Lamphear      Have a great Fourth
      11  07/13 11:58CDT Dave Lamphear      Re: The next projec
    
Because sortm sorted by the field From: before the date, the messages from Al came first (because "a" comes before "d" in the alphabet).