Computing.Net > Forums > Unix > Sort Order Presevation

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Sort Order Presevation

Reply to Message Icon

Original Message
Name: Graeme Conn
Date: September 2, 2005 at 07:16:34 Pacific
Subject: Sort Order Presevation
OS: unix
CPU/Ram: B.11.11 U 9000/800
Comment:

Take the simple input file:

5BBBBBBBBBBBBBBB1
5BBBBBBBBBBBBBBB2
2BBBBBBBBBBBBBBB1
2BBBBBBBBBBBBBBB3
2BBBBBBBBBBBBBBB2

I 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
5BBBBBBBBBBBBBBB2

I want:
2BBBBBBBBBBBBBBB1
2BBBBBBBBBBBBBBB3
2BBBBBBBBBBBBBBB2
5BBBBBBBBBBBBBBB1
5BBBBBBBBBBBBBBB2

Help !



Report Offensive Message For Removal


Response Number 1
Name: nails
Date: September 2, 2005 at 08:58:44 Pacific
Reply:

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


Report Offensive Follow Up For Removal

Response Number 2
Name: Graeme Conn
Date: September 2, 2005 at 09:14:41 Pacific
Reply:

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.


Report Offensive Follow Up For Removal

Response Number 3
Name: Luke Chi
Date: September 6, 2005 at 14:52:12 Pacific
Reply:

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


Report Offensive Follow Up For Removal

Response Number 4
Name: Luke Chi
Date: September 6, 2005 at 15:22:19 Pacific
Reply:

cat -n k | sort -k 1.9,1.22 -k 1.8,1.8 -k 1|awk ' { print $2 }'

Luke Chi


Report Offensive Follow Up For Removal







Post Locked

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


Go to Unix Forum Home



Results for: Sort Order Presevation

Sorting in C
    Summary: Hi, This might help you. #include <stdio.h> #define SWAP(a,b) a+=b;b=a-b;a-=b int main() { int num1, num2; printf("Please enter two numbers to Sort : \n"); scanf("%d %d", &num1, &num2); print...
www.computing.net/answers/unix/sorting-in-c/5628.html

Numerical sort with sort
    Summary: Well, it's not exactly what I'm looking for, but probably it's my fault because it's a bit complicated for me too. To be honest, the first field was added by me, accordingly to a trick that (maybe) yo...
www.computing.net/answers/unix/numerical-sort-with-sort/7380.html

Send mail on UNIX
    Summary: Hi, I have a question. how do we send a mail on one line command?? like it should include the.... recipent, subject, body, I found some sources online but, it doesnt send it right away.. I tried pi...
www.computing.net/answers/unix/send-mail-on-unix/4064.html








Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History






Data Recovery Software