On Tue, Dec 21, 1999 at 10:30:14AM -0600, Hanigan Family wrote:
| Is there away to use sort
| so if the field it is sorting by
| is the same it leaves them in
| the current order or do I have
| to right my own program to do it?

Wow. I spent some time parsing that one...

In short:
        - the sort command doesn't necessarily to stable sorts
          (where records with equal keys are not reordered)
        - you can work around this

Simply prepend a leading leading with counts upwards, numericly.
Thus:
        1 fields of line 1
        2 fields on the next line
        3 etc ...

Then supposing your desired sort command was
        sort +2
to sort lexically on the third column. Then with the prepended count
this becomes
        sort +3 -n +0
so that then the third field is equal, it falls back to the prepended
field and compares numerically, which will coerce it into keeping the
original order. Then after sorting, remove the first field.

So, for example,
        sort +2 <rawdata >sorteddata
could become
        awk '{print NR " " $0}' <rawdata \
        | sort +3 -n +0 \
        | cut -d' ' -f2- >sorteddata
(awk to prepend numbers, cut to rip them off afterwards).

Easy, eh?
-- 
Cameron Simpson, DoD#743        [EMAIL PROTECTED]    http://www.zip.com.au/~cs/

That's about as good as when one of my students left me a note signed
'anon.'--written on personalized stationery.
        - Ayse Sercan <[EMAIL PROTECTED]>


-- 
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.

Reply via email to