Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Take the simple input file:
5BBBBBBBBBBBBBBB1
5BBBBBBBBBBBBBBB2
2BBBBBBBBBBBBBBB1
2BBBBBBBBBBBBBBB3
2BBBBBBBBBBBBBBB2I need to sort this on position 2-16 and postions 1-1 which is a record type. However I need to preserve the sort order, when I apply the following sort -k 1.2,1.15 -k 1.1,1.1 on this file, I end up with :
2BBBBBBBBBBBBBBB1
2BBBBBBBBBBBBBBB2
2BBBBBBBBBBBBBBB3
5BBBBBBBBBBBBBBB1
5BBBBBBBBBBBBBBB2I want:
2BBBBBBBBBBBBBBB1
2BBBBBBBBBBBBBBB3
2BBBBBBBBBBBBBBB2
5BBBBBBBBBBBBBBB1
5BBBBBBBBBBBBBBB2Help !

Obviously, sort doesn't guarantee the order of any columns or fields not sorted on. One trick is to add a dummy column to your file, perform a secondary sort, and, finally, remove the column:
cat -n myfile|sort -k 2.2,1.15 -k 2.1,1.1 -k 1|awk ' { print $2 } '
The above command should deliver the desired output.
See a further discussion here:
http://www.unixreview.com/documents/s=9811/ur0506h/ur0506h.html

thanks, exactlty what I was thinking. I was hoping the unix sort would be clever enough. This was not an issue on the old ibm mainframe sort utility....mmmmmm.

It's stupid, but it's true that sort "guarantees" the order of any columns or fields not sorted on. We just can't disable it.
If you specify -r which is descending order, the order of any columns or fields not sorted on will be in decending order.
If you DON'T specify -r, the order of any columns or fields not sorted on will be in ascending order.
Luke Chi

![]() |
Awk withing an awk?
|
replace a column with a n...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |