How to sort when there is variable length decimal points.?

Hi Experts,
Quick quesion:

I want to sort this in the file , but not working, when using # sort file name

305.932 
456.470 
456.469 
456.468 
456.467 
172.089 
456.467 
456.466 
456.465 
111.573 
111.578 
111.572 
111.572 
87.175  
87.174  
75.898  
75.895  
75.894  
75.894  
75.893  
87.211  
86.905  
86.905  
144.130 
144.179 

I am trying:

sort -rn file 

The output is not comming in sorted output. please advise,

In GNU sort there is a -g (general-numeric-sort) option. Try using it.

1 Like

When I try it on OS X, I get the output:

456.470 
456.469 
456.468 
456.467 
456.467 
456.466 
456.465 
305.932 
172.089 
144.179
144.130 
111.578 
111.573 
111.572 
111.572 
87.211  
87.175  
87.174  
86.905  
86.905  
75.898  
75.895  
75.894  
75.894  
75.893 

which looks right to me.
What OS are you using? What output do you get?

1 Like

Something is not quite right: POSIX on sort options (the group that defines for UNIX OS providers what things have to do to get certification) when sort has the -n option:

-n
Restrict the sort key to an initial numeric string, consisting of optional <blank> characters, optional minus-sign, and zero or more 
digits with an optional radix character 
and thousands separators (as defined in the current locale), which shall be sorted by arithmetic value. An empty digit string 
shall be treated as zero. 
Leading zeros and signs on zeros shall not affect ordering.

In your locale a radix character is the dot (or period). So with almost any modern UNIX/Linux this should work. What makes you think it does not work?

1 Like

Sorry my bad! I am able to figure it out now,
I was trying sorting this values which were in column 5th, However at a point the 4rth and 5th column touching together and it was making one column sort , so the output was not coming right.

Yoda, Don, thanks. Don, You are right, OS X , is correct. and the command is correct. my data file had problem.

Thanks Jim , able to figure it out.

I was trying sort -rnk 5 and the output was not coming as expected:

cmihpu28 :  C2PDI03 :: 75.894    Days::
cmihpu29 :  C2PDI10 :: 75.893    Days::
cmihpu38 :  C2PDI05 :: 75.893    Days::
talhpu27 :  A2DAS   :: 73.242    Days::
talhpu28 :  C2SPO   :: 52.292    Days::
cmihpu82 :  POSAS   :: 49.152    Days::
cmihpu82 :  ARTDAS  :: 34.176    Days::
cmihpu83 :  ARTDB   :: 11.386    Days::
talhpu27 :  A3TAS   :: 0.647     Days::
talhpu28 :  B3TAS   :: 0.646     Days::
cmihpu12 :  COMWP981:: 318.889   Days::
cmihpu12 :  COMWD981:: 339.943   Days::
cmihpu81 :  DVFLP981:: 456.457   Days::

Actually sort -rn workingg as expected. The problem with the file which has disorder in few columns that I was trying ,

sort -rnk 5
sort -rn file1
456.470
456.469
456.468
456.467
456.467
456.466
456.465
305.932
172.089
144.179
144.130
111.578
111.573
111.572
111.572
87.211
87.175
87.174
86.905
86.905
75.898
75.895
75.894
75.894
75.893
# uname -a
HP-UX srvhp004 B.11.23 U ia64 899690123 unlimited-user license

Thanks all.