LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   command to sort by floats in fourth column of standard output (http://www.linuxquestions.org/questions/linux-software-2/command-to-sort-by-floats-in-fourth-column-of-standard-output-563718/)

jhwilliams 06-22-2007 11:05 AM

command to sort by floats in fourth column of standard output
 
I have a command that prodcues a list of servers...

Code:

anvil                ia64linux      4  4.19    63.2G    26.7G  136.7G    1.0M
carbon              ia64linux      4  0.03    15.2G    1.3G    2.4G      0.0
cesium              ia64linux      4  3.99    9.9G    1.0G    5.0G      0.0
delaware            aix43          4  0.03    16.0G    1.3G    22.4G    4.1M
flame                lx24-amd64    8  8.62    3.9G    3.3G    16.0G    1.5G
...

The fourth column is the cpu load. I would like to sort the output by the load. Is there anyway to pipe this output and then sort it by the number in the fourth column??

Thanks!
Jameson

druuna 06-22-2007 11:25 AM

Hi,

The sort command should be able to do just that:
Code:

$ sort -k 3 testfile
delaware            aix43          4  0.03    16.0G    1.3G    22.4G    4.1M
carbon              ia64linux      4  0.03    15.2G    1.3G    2.4G      0.0
cesium              ia64linux      4  3.99    9.9G    1.0G    5.0G      0.0
anvil                ia64linux      4  4.19    63.2G    26.7G  136.7G    1.0M
flame                lx24-amd64    8  8.62    3.9G    3.3G    16.0G    1.5G

Take a look at the -k option in the sort manpage.

Hope this helps.

jhwilliams 06-22-2007 12:31 PM

sort output by float, not string?
 
Very good! Thanks so much. One issue:

Lines with loads > 9.99, i.e. 10.00+ (two digit integral loads) are sorted in the middle of one digit integral loads.

That is to say..
Code:

host1 1.23
host2 13.00
host3 2.57

etc...

Perhaps there is a way to read these values as floats, not as string?

Thanks!

druuna 06-22-2007 12:54 PM

Hi,

Try this instead: sort -k4g infile (btw: the sort column is 4, not 3 as in my previous post).

Hope this helps.

jhwilliams 06-22-2007 02:27 PM

There it is! Thanks for your help.


All times are GMT -5. The time now is 05:39 AM.