Unix Programming - number sort

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2006 > number sort





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author number sort
whcui@yahoo.com

2006-01-29, 9:32 pm

Hi,


Here is some data:
aaa,-1.09221e+06,-30955,35.569
bbb,1.13284e+06,67479,16.735
ccc,1.78407e+06,93986,20.9255
ddd,25.92,4,6.48
eee,108.65,25,4.32


I want to sort them by the second column. "sort -t, -n -k 2,2" does not
work. Maybe sort does not understand "e+06". Can any one help? I am
using /bin/sh on a solaris 8 server.


Thanks!

Robert Harris

2006-01-29, 9:32 pm

whcui@yahoo.com wrote:
> Hi,
>
>
> Here is some data:
> aaa,-1.09221e+06,-30955,35.569
> bbb,1.13284e+06,67479,16.735
> ccc,1.78407e+06,93986,20.9255
> ddd,25.92,4,6.48
> eee,108.65,25,4.32
>
>
> I want to sort them by the second column. "sort -t, -n -k 2,2" does not
> work. Maybe sort does not understand "e+06". Can any one help? I am
> using /bin/sh on a solaris 8 server.
>
>
> Thanks!
>

Yes, the -n option doesn't recognise e+06. GNU sort has a -g option that
does.

Robert
Bill Marcum

2006-01-29, 9:32 pm

On 27 Jan 2006 07:02:21 -0800, whcui@yahoo.com
<whcui@yahoo.com> wrote:
> Hi,
>
>
> Here is some data:
> aaa,-1.09221e+06,-30955,35.569
> bbb,1.13284e+06,67479,16.735
> ccc,1.78407e+06,93986,20.9255
> ddd,25.92,4,6.48
> eee,108.65,25,4.32
>
>
> I want to sort them by the second column. "sort -t, -n -k 2,2" does not
> work. Maybe sort does not understand "e+06". Can any one help? I am
> using /bin/sh on a solaris 8 server.
>

Try -g instead of -n. I'm not sure if that is a GNU enhancement of the
sort command. If your sort does not support -g:

while IFS=, read c1 c2 rest_of_line; do
printf "%s,%d,%s\n" "$c1" "$c2" "$rest_of_line"
done | sort -t, -n -k 2,2


--
Life's the same, except for the shoes.
- The Cars
William James

2006-01-29, 9:32 pm

whcui@yahoo.com wrote:
> Hi,
>
>
> Here is some data:
> aaa,-1.09221e+06,-30955,35.569
> bbb,1.13284e+06,67479,16.735
> ccc,1.78407e+06,93986,20.9255
> ddd,25.92,4,6.48
> eee,108.65,25,4.32
>
>
> I want to sort them by the second column. "sort -t, -n -k 2,2" does not
> work. Maybe sort does not understand "e+06".


ruby -e 'puts ARGF.sort_by{|x|x.split(",")[1].to_f}' myfile

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com