How to sort on exactly one field only

Rob Lemley rob at b15.INGR.COM
Thu Oct 25 08:22:54 AEST 1990


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:

cat <<EOF | sort +1 -2 +2r
appl1  3  Submitted
appl2  4  Submitted
appl1  3  Started
appl3  5  Submitted
appl2  4  Started
appl1  3  Finished(0)
appl2  4  Finished(7)
appl3  5  Started
appl3  5  Finished(3)
EOF

The +2r sorts the third field in reverse order.

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.

--
-Rob Lemley, System Consultant, Scanning Software, Intergraph, Huntsville, AL
 ...!uunet!ingr!b15!rob   OR   b15!rob at INGR.COM
 205-730-1546



More information about the Comp.unix.questions mailing list