How to sort on exactly one field only
Joe Wells
jbw at bucsf.bu.edu
Thu Oct 25 19:03:33 AEST 1990
In article <1757 at b15.INGR.COM> rob at b15.INGR.COM (Rob Lemley) writes:
In <1094 at massey.ac.nz> ARaman at massey.ac.nz (Anand Venkata Raman) writes:
>I want to sort a file exclusively on field #2.
. . .
>I tried using sort +1 -2, but that doesn't seem to deter sort from looking
>at field #3.
Anand, you cannot deter sort from looking at field three. Try this:
[stuff deleted]
The man page clearly states that you cannot cause sort to ignore a field
or preserve relative line ordering. From the sort(1) man page
(sys V release 3), under DESCRIPTION:
When there are multiple sort keys, later keys are compared only
after all earlier keys compare equal. Lines that otherwise
compare equal are ordered with all bytes significant.
^^^^^^^^^^^^^^^^^^^^^
and (from WARNINGS):
sort does not guarantee preservation of relative line ordering
on equal keys.
I'm including a fix that someone posted a while back to make sort preserve
relative line ordering on equal keys.
Enjoy,
--
Joe Wells <jbw at bu.edu>
----------------------------------------------------------------------
One of our users needed a version of sort (we're running
4.3bsd) that is stable; ie, lines considered equal are
grouped together but not otherwise rearranged; For example,
0 1
0 0
sorted on the 0th field would not be rearranged.
As distributed, sort's behavior is (from the man page)
"Lines that otherwise compare equal are ordered with all
bytes significant".
We puzzled through sort and came up with a "fix". One
question about sort though, any idea why ibuf[256] rather
than ibuf[7], since it never seems to merge more than
7 files at a time?
Thanks,
Ron Stanonik
stanonik at nprdc.arpa
Diffs to make sort stable
*** /usr/src/usr.bin/sort.c Tue Jun 3 16:59:43 1986
--- sort.c Tue Dec 20 09:00:45 1988
***************
*** 35,40 ****
--- 35,41 ----
int mflg;
int cflg;
int uflg;
+ int Sflg;
char *outfil;
int unsafeout; /*kludge to assure -m -o works*/
char tabchar;
***************
*** 206,211 ****
--- 207,216 ----
dirtry[0] = *++argv;
continue;
+ case 'S':
+ Sflg++;
+ continue;
+
default:
field(++*argv,nfields>0);
break;
***************
*** 673,678 ****
--- 678,685 ----
}
if(uflg)
return(0);
+ if(Sflg)
+ return(i < j ? 1 : -1);
return(cmpa(i, j));
}
More information about the Comp.unix.questions
mailing list