Xref: utzoo comp.unix.shell:716 comp.unix.questions:26488 Path: utzoo!attcan!uunet!know!zaphod.mps.ohio-state.edu!wuarchive!emory!att!bu.edu!nntp-read!jbw From: jbw@bucsf.bu.edu (Joe Wells) Newsgroups: comp.unix.shell,comp.unix.questions Subject: Re: How to sort on exactly one field only Message-ID: Date: 25 Oct 90 09:03:33 GMT References: <1094@massey.ac.nz> <1757@b15.INGR.COM> Sender: news@bu.edu.bu.edu Followup-To: comp.unix.shell Organization: Boston University Computer Science Department Lines: 89 In-reply-to: rob@b15.INGR.COM's message of 24 Oct 90 22:22:54 GMT In article <1757@b15.INGR.COM> rob@b15.INGR.COM (Rob Lemley) writes: In <1094@massey.ac.nz> ARaman@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 ---------------------------------------------------------------------- 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@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)); }