Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort by specified field(s) doesn't work as expected !!!

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
I'm using ksh in Solaris ...

Say I have a simple 4 line file:
30 XB A
30 XB B
30 XB C
[COLOR=red yellow]30 XB A[/color]

I want to sort on the 1st column only and not have another
column (i.e. - 3rd column) influence my result:

I run something like...
sort -k0,1 file

...but, my return result is always:
30 XB A
[COLOR=red yellow]30 XB A[/color]
30 XB B
30 XB C

Where the last column seems to be influencing my result - I don't want that 3rd columnn to effect the sort - what am I doing wrong?
 
Unfortunately, the unix sort command does not guarantee the order of any columns not sorted on. One kludge is to add a new column, and secondarily sort on that to restore the order of the other columns, and then use awk to remove the column:

Code:
cat -n datafile|sort -k 2,2 -k 1,1 |awk ' { $1=""; print $0 } '


 
olded - thanks for the response ...

That blows me away that there's no guarantee on those columns not explicitly sorted on ...

No where in the man pages suggests this (not that I have found). The only thing of worth mentioning is:
"If no sort key is specified, a default sort key of the entire line is used." Which to me implies that if a sort key is supplied, the entire line is NOT used !!!!

BTW - your kludge works nicely; I might consider this after I look into this a little more ...
 
Not sure about Solaris; on HP-UX 11.23 I see this:
man sort said:
... Lines that otherwise compare equal are ordered with all bytes significant. If all the specified keys compare equal, the entire record is used as the final key.
 
Same on SCO OpenServer (either 5.0.X or 6.0.0):
man sort said:
Lines that otherwise compare equal are ordered with all bytes significant.
 
Ahhhh ... found the disclaimer - it's at the very bottom of the man page ....

sort's man page said:
sort does not guarantee preservation of relative line ordering on equal keys.


I will use the wonderful suggestion olded provided, only modified to avoid the leading space (and thus not affecting the column positions)

cat -n datafile|sort -k 2,2 -k 1,1 |awk ' { $1=""; print substr($0,2)} '

 
The -n option for cat isn't present in all *nix flavors.
What about this ?
Code:
pr -tn -e datafile | sort -k 2,2 -k 1,1 | cut -f2-

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Another good one from PHV ...

Q?: what's the 2nd "-" for in the cut cmd?

is this the file operand, which in this case is standard input?
 
Q?: what's the 2nd "-" for in the cut cmd?
As usual the answer is: man cut

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top