sorting numbers with sort -n


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sorting numbers with sort -n
# 1  
Old 07-21-2010
sorting numbers with sort -n

Looking for help for sort, I learned that for sorting numbers I use:
sort -n
but it seems that that is not enough when you have numbers like 0.2000E+7 for example, sort -n will not worry about the E+7 part, and will just sort the numbers like 0.2000.

Exapmle:

Code:
 cat example.txt
.91000E+07
.92000E+04
.93000E+06
.94000E+02
.95000E+09
.96000E+07

sort -n example.txt
.91000E+07
.92000E+04
.93000E+06
.94000E+02
.95000E+09
.96000E+07

How can I fix that?Smilie
# 2  
Old 07-21-2010
one way:

Code:
#  awk '{printf("%f\n",$1)}' example.txt | sort -n
94.000000
9200.000000
930000.000000
9100000.000000
9600000.000000
950000000.000000

HTH
This User Gave Thanks to Tytalus For This Post:
# 3  
Old 07-21-2010
Is that the only way to do it?
# 4  
Old 07-21-2010
What is not ok with this solution? Afaik sort can't handle exponential values in this syntax.
# 5  
Old 07-21-2010
What is I have a bigger file with more columns? Let's say I want to sort by the second column and out put the whole sorted file... I tried:

Code:
awk '{printf($1, "%f\n",$2, $3, $4, $5)}' file.txt | sort -n -k 2.1 > sortfile.txt

but it damaged the file... I don't even know how to exmplain the output...
# 6  
Old 07-21-2010
This seems to work:

Code:
sort -t+ -nk2 file

(at least for values > 0)
This User Gave Thanks to Scott For This Post:
# 7  
Old 07-21-2010
Quote:
Originally Posted by scottn
This seems to work:

Code:
sort -t+ -nk2 file

(at least for values > 0)

Yes, that worked ... for a different column I use

Code:
sort -t+ -nk2 -k 2 file

for example and it works! Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Sorting scientific numbers with sort

Hey everybody, I'm trying to sort scientific numbers in a descending order using the command sort -gr <file>. It works fine on a Linux-Server, but doesn't on my macbook pro with OS X 10.10.3 (Yosemite). I tried to sort the following: 6.38e-10 6.38e-10 1.80e-11 1.00e-10 1.48e-12 And... (9 Replies)
Discussion started by: plebs
9 Replies

2. Shell Programming and Scripting

Help with Sorting numbers in a file

Hi Guys, Would appreciate some help on sorting numbers on a file using the sort command. I have tried this and it's not sorting properly. what am i missing? cat testing_sort 1:21 4:18 2:17 7:14 9:19 3:12 0:16 8:13 5:20 6:15 10:11 sort -t: -nk1,1 -nk2,2 testing_sort (4 Replies)
Discussion started by: Apollo
4 Replies

3. Shell Programming and Scripting

Help: Sorting the numbers without using sort command

Hi, I've got two arrays 1 3 5 7 2 4 6 8 and i need to write a shell script to get the output 1 2 3 4 5 6 7 8 without using sort or bubble sort. (1 Reply)
Discussion started by: web2moha
1 Replies

4. Shell Programming and Scripting

Sorting mixed numbers and letters

Hello, I have a file such as this: chr1 chr2 chr1 chr2 chr3 chr10 chr4 chr5 chrz chr1AI want to sort it, I use this command: sort -k1 -th -n testfilebut I get this output, how can I fix this? chr1 chr1 chr10 chr1A chr2 chr2 (3 Replies)
Discussion started by: Homa
3 Replies

5. UNIX for Dummies Questions & Answers

Sorting files ending in numbers with "sort"

I have a group of files that I need to be sorted by number. I have tried to use the sort command without any luck. ls includes* includes1 includes10 includes11 includes12 includes2 includes3 includes4 includes5 includes6 includes7 includes8 includes9 I have tried ls includes*... (6 Replies)
Discussion started by: newbie2010
6 Replies

6. Shell Programming and Scripting

Sorting numbers containing symbols

hi all, i need help on sorting data. i have a file as below /home/oracle $ cat 234.txt +1234 -2356 -1001 +231 0023 -0987 +19000 65487 6 after sorting i want the output as below -2356 -1001 (2 Replies)
Discussion started by: mahesh1987
2 Replies

7. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

8. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

9. UNIX for Dummies Questions & Answers

sorting ASCII string containing numbers

I have the following output where I need to sort the second column numerically (starting with IBMULT3580-TD10 and ending in IBMULT3580-TD123) Drv DriveName 0 IBMULT3580-TD13 1 IBMULT3580-TD18 2 IBMULT3580-TD14 3 IBMULT3580-TD10 4 IBMULT3580-TD11 5 IBMULT3580-TD17 ... (8 Replies)
Discussion started by: GKnight
8 Replies

10. Shell Programming and Scripting

Trouble sorting numbers embedded

Hi All, First time poster. I have a text file that as many entries like below (single line): egrep RspTime conlb.txt |more S(xxx.xxx.xxx.xxx:5050:UP) Hits(13358983, 2/sec, P) ATr(2) Mbps(0.07) BWlmt(0 kbits) RspTime(16.50 ms) ... I am trying to sort on the RspTime from highest to lowest... (4 Replies)
Discussion started by: mycrew2008
4 Replies
Login or Register to Ask a Question