Unix Shell - number sort

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > 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
learner

2006-01-29, 9:31 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 -n -k 2,2" does not
work. Can any one help? I am using /bin/sh on a solaris 8 server.

Thanks!

Chris F.A. Johnson

2006-01-29, 9:31 pm

On 2006-01-27, learner 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 -n -k 2,2" does not
> work. Can any one help? I am using /bin/sh on a solaris 8 server.


How do you define column?

Have you looked at the -t option to sort?

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
learner

2006-01-29, 9:31 pm

I tried "sort -t, -n -k 2,2". It does not work. I guess"sort" does not
understand "e+06" part in the number.

Stephane Chazelas

2006-01-29, 9:31 pm

On 27 Jan 2006 06:36:07 -0800, learner 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 -n -k 2,2" does not
> work. Can any one help? I am using /bin/sh on a solaris 8 server.

[...]

sort doesn't do floating point.

GNU sort does

sort -t, -k 2,2g

Convert it to an integer or use PERL if you don't have GNU sort

PATH=$(getconf PATH):$PATH
export PATH

awk -F, '{printf("%d,%s\n", $2 * 100, $0)}' |
sort -t, -k 1,1n |
cut -d, -f2-


perl -e '
print sort {(split /,/, $a)[1] <=> (split /,/, $b)[1]} <>'

--
Stephane
Chris F.A. Johnson

2006-01-29, 9:31 pm

On 2006-01-27, learner wrote:
> I tried "sort -t, -n -k 2,2". It does not work. I guess"sort" does not
> understand "e+06" part in the number.


Part in what number? Please read <http://cfaj.freeshell.org/google>.


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
William James

2006-01-29, 9:31 pm

learner 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 -n -k 2,2" does not
> work. Can any one help? I am using /bin/sh on a solaris 8 server.



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