Sort Decimal number in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort Decimal number in UNIX
# 1  
Old 06-27-2013
Sort Decimal number in UNIX

Hello Everyone,

In one of my script, I would like to sort the decimal numbers.

For e.g.

If I have numbers like
Code:
1.0 1.1 1.2 2.0 2.1 3.0 4.0 5.0 6.0 7.0 7.1 7.10 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9

I would like to sort them

Code:
1.0 1.1 1.2 2.0 2.1 3.0 4.0 5.0 6.0 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10

that means, 1.10 is greater that 1.2.

Can you please help me out to achieve this.

Kind Regards,
Sachin

Moderator's Comments:
Mod Comment Use code tags, thanks; see PM.

Last edited by zaxxon; 06-27-2013 at 05:50 AM.. Reason: code tags
# 2  
Old 06-27-2013
Where are these numbers stored? If they appear line by line in a file, you can use sort command.

Code:
sort -nk1,1 filename

If you have the list of numbers in a variable (say LIST), you can use

Code:
echo $LIST | tr " " "\n" | sort -nk1,1


Please discard this solution. I read the problem as "sorting decimals" from the subject. The problem is slightly different here.

Last edited by krishmaths; 06-27-2013 at 05:56 AM.. Reason: Wrong solution
# 3  
Old 06-27-2013
Try this:
Code:
sort -t. -k1,1n -k2,2n

If it's one line, you'll have to split it first:
Code:
tr ' ' '\n' < file

# 4  
Old 06-27-2013
try..

Code:
 
cat filename| tr ' ' '\n' | sort -t. -nk1,1 -k2,2|tr '\n' ' '

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 (4 Replies)
Discussion started by: rveri
4 Replies

2. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

3. Shell Programming and Scripting

Matching a decimal number?

Hi everyone! Easy question for everyone. I'm trying to run a command line to find an exact match of a decimal number within a file. The number can be a positive OR negative number. For instance, if I want to find only the number -1 in the file that has: -17.6 -17 -16.3 -16.2 -15.7 -15.3... (6 Replies)
Discussion started by: lucshi09
6 Replies

4. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

5. Homework & Coursework Questions

Unix/Linux Math Decimal to Whole Number Format?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The problem? I hope I fill this out correctly. I have a program that runs like a cash register. It works and... (6 Replies)
Discussion started by: Iceman69
6 Replies

6. Shell Programming and Scripting

Add zero in decimal number

echo "scale=2; 282.73/640" | bc This will print .44 How to make the variable as 0.44 (2 Replies)
Discussion started by: sandy1028
2 Replies

7. Shell Programming and Scripting

how to sort date in decimal values uptp two digits

Hi all, there is a data in a file wich loks likes 00:00:49|24.48| 00:01:49|22.83| 00:02:49|22.07| 00:03:49|20.72| 00:04:49|21.28| 00:05:49|21.22| 00:06:49|21.38| 00:07:49|20.93| 00:08:49|21.27| 00:09:49|20.65| 00:10:49|19.42| 00:11:49|21.93| 00:12:49|20.62| 00:13:49|20.23|... (3 Replies)
Discussion started by: jojo123
3 Replies

8. Shell Programming and Scripting

Test decimal number

Hi, I would like test if a number is a decimal number or not (9 Replies)
Discussion started by: francis_tom
9 Replies

9. Shell Programming and Scripting

How to sort decimal values in bash

Hi, I have a list of values from associative array from 0,..till 1.0000. I tried various sort options; sort -g, sort -nr but it still couldnt work. In other words, the numbers are not sorted accordingly. Please help. Thanks. (1 Reply)
Discussion started by: ahjiefreak
1 Replies
Login or Register to Ask a Question