Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort over two columns with exponents in bash

I need to sort some data from file "test" (example of data):

1.03073 0.000193333 2.02417 2.15578
1.03073 0.0002 2.12005 2.14534
1.03073 1.33333e-05 100 0
1.03073 2e-05 100 0

so that it is sorted over first column first, and over second afterwards. I tried with

sort -n -k1 -k2 -u test >test1 

but it messes up because he does not see "e-5" part. Any ideas?

like image 753
Luka Petrović Avatar asked Dec 30 '25 17:12

Luka Petrović


1 Answers

You can use -g:

sort -k1,2 -g file

From man sort:

-g, --general-numeric-sort

compare according to general numerical value

Test

$ cat a
1.03073 0.000193333 2.02417 2.15578
1.03073 0.0002 2.12005 2.14534
1.03073 1.33333e-05 100 0
1.03073 2e-05 100 0
1.03073 2e-04 100 0
1.03073 2e-06 100 0

$ sort -k1,2 -g a
1.03073 0.000193333 2.02417 2.15578
1.03073 0.0002 2.12005 2.14534
1.03073 1.33333e-05 100 0
1.03073 2e-04 100 0
1.03073 2e-05 100 0
1.03073 2e-06 100 0
like image 115
fedorqui 'SO stop harming' Avatar answered Jan 01 '26 10:01

fedorqui 'SO stop harming'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!