On Tue, 9 Jan 2007, Bill McCarthy wrote:
On Tue 9-Jan-07 4:11am -0600, Vigil wrote:
This works on my admittedly small test set:
:%!sort -k3 -t/
You're making several unstated assumptions.
(1) The records are already sorted by month and day.
I would have thought that, too, but sorting this:
01/04/2007 blah
01/03/2007 blah
12/30/2006 blah
07/05/2003 blah
02/04/2007 blah
02/04/2006 blah
14/32/1996 blah
02/04/1996 blah
give this:
02/04/1996 blah
14/32/1996 blah
07/05/2003 blah
02/04/2006 blah
12/30/2006 blah
01/03/2007 blah
01/04/2007 blah
02/04/2007 blah
which, as you can see, has sorted by month and day, too,
or is that just happenstance?
I had assumed the latter - but read on.
(3) Everything to the right of the year is the same in each
record.
Ah, I see why. My example sorts beginning at that column, not just on that
column alone.
I thought I knew the unix sort utility fairly well. I'm a
bit baffled.
I wrote a simple program to generate 1,000 records with just
random dates (month 1-12, day 1-30, year 1976-2007).
Sorting with:
:%!sort -t/ -k3
and
:%!sort -t/ -k3.1n,3.4 -k1n,2
produced identical correct results!
arun> Lines with same 3rd field are sorted lexicographically. So, if you
have MM/DD/YYYY format, that should be good. If you have DD/MM/YYYY, it
cannot be (Try adding 31/11/1996 and 01/12/1996).
I then modified my program to add a comma and a random
letter. Of course, your method failed since your 3rd field
includes the trailing 2 characters.
But modifying your method to:
:%!sort -t/ -k3n
worked perfectly ????
arun> Again, because of the lexicographic sort of the column for identical
years. Replace "02/04/1996" with "2/04/1996" and see the difference.
regards,
-Arun
BTW, I'm using: sort (GNU coreutils) 5.3.0
on Windows XP. I know this discussion is a bit off topic
but I have found it fascinating. Thanks, Vigil, for bring
it up.
--
Best regards,
Bill