1757495 Members
3163 Online
108862 Solutions
New Discussion юеВ

sort problem

 
SOLVED
Go to solution
tim zollers
Occasional Advisor

sort problem

I have a file with contents similar to the following:

S:997
S:998
C:998
S:999
C:999
S:1000
S:1001
S:1002
C:1000

I'm trying to sort based on the single character value in the first column and a numeric sort on the second column. I've tried the following command sorting the numeric column first and then the character column. I have also tried sorting (correctly) on the numeric column into a file and then applying the second sort to the contents of that file, but the numeric portion is in each instance reordered in ascending ascii order even though the second sort instructions should be limited to the first column.

My sort command:
sort -t: -k 2n,2n | sort -t: -k1.1,1.1

Results in:
C:1000
C:998
C:999
S:1000
S:1001
S:1002
S:997
S:998
S:999

Desired result is:
C:998
C:999
C:1000
S:997
S:998
S:999
S:1000
S:1001
S:1002

All suggestions appreciated.
thanks,
tim
5 REPLIES 5
Ian Lochray
Respected Contributor
Solution

Re: sort problem

How about ...
sort -t: -k 1,1 -k 2n,2 filename
Robin Wakefield
Honored Contributor

Re: sort problem

Hi Tim,

This should sort if for you:

sort -t: -k1,1 -k2,2n filename

Rgds, Robin
Gerrit Kistemaker
New Member

Re: sort problem

try:

sort -t: -k1,1 -k2n,2n

Gerrit
Dietmar Konermann
Honored Contributor

Re: sort problem

sort -t: -k 1,1 -k 2n,2n

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
tim zollers
Occasional Advisor

Re: sort problem

Thanks to all. I thought I had tried that in one of my many iterations.

Problem solved.