# Sorting exponential notation

**URL:** <https://community.unix.com/t/sorting-exponential-notation/273258>\
**Category:** Shell Programming and Scripting\
**Created:** [September 13, 2010, 4:43pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258 "2010-09-13T16:43:32Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 4:43pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/1 "2010-09-13T16:43:32Z")

</div>

Hi,  
I just want to sort my file with exponential notation. For example:

```nohighlight
1;2;4;s
1;5e-01;4;s
1;1;4;s

```

I used sort -gk2, but it does not sort in the correct way. What's wrong?

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 13, 2010, 5:06pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/2 "2010-09-13T17:06:02Z")

</div>

Try:

```nohighlight
sort -t\; -k2,2g

```

---

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 5:38pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/3 "2010-09-13T17:38:01Z")

</div>

Thanks ..it works

---------- Post updated at 04:38 PM ---------- Previous update was at 04:24 PM ----------

the numbers stands for the columns right? 2g = second column wtih exp. notation

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 13, 2010, 5:43pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/4 "2010-09-13T17:43:20Z")

</div>

2,2 means the sort key starts at column 2 and ends at column2, so col 2 only.

---

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 5:46pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/5 "2010-09-13T17:46:47Z")

</div>

ok thanks .. when I want another column, e.g. only 3 the -k,3,3g

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 13, 2010, 5:49pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/6 "2010-09-13T17:49:09Z")

</div>

that would be -k3,3g without the comma after the k.

---

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 6:03pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/7 "2010-09-13T18:03:02Z")

</div>

right sorry ..many thanks for your help

---------- Post updated at 05:03 PM ---------- Previous update was at 04:50 PM ----------

Sorry for asking again: But I tried it on my data and it does not function;(.  
So my real data looks like:  
XX300890;4.27691979504843e-01;4;KL144469220  
XY300890;4.27691979504843e-05;4;KL144469220  
XY300890;4.27691979504843e-03;4;KL144469220

And I use your code..to sort the second column... but I do not get the expected result???

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 13, 2010, 6:06pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/8 "2010-09-13T18:06:25Z")

</div>

Strange, I get:

```nohighlight
$ sort -t\; -k2,2g infile
300890;4.27691979504843e-05;4;144469220
300890;4.27691979504843e-03;4;144469220
300890;4.27691979504843e-01;4;144469220

```

Which looks alright to me. Did you use: -t\; ?

---

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 6:19pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/9 "2010-09-13T18:19:14Z")

</div>

sorry I edit the text ..i think the reason is that i some columns are numbers and in others there are strings:

```nohighlight
nils$ sort -t\; -k2,2g test.txt
 
XX10914755;0.0879836449881873-e10;1;33854866;CSMD2;T;C;I_2321282;POEA;8;43212072;43212115
XX12745618;0.00069063786440612;1;4019438;hCG_2036596;T;C;I_2321282;POEA;8;43212072;43212115
XX1547793;0.0000076396450281944;1;57739400;DAB1;T;C;I_2321282;POEA;8;43212072;43212115
XX3762296;0.00334938423169702;1;31003973;LAPTM5;A;G;I_2321282;POEA;8;43212072;43212115

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 13, 2010, 6:32pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/10 "2010-09-13T18:32:49Z")

</div>

Hi, I think there is an error in your input file, no? If I use:

```nohighlight
XX10914755;0.0879836449881873e-10;1;33854866;CSMD2;T;C;I_2321282;POEA;8;43212072;43212115
XX12745618;0.00069063786440612;1;4019438;hCG_2036596;T;C;I_2321282;POEA;8;43212072;43212115
XX1547793;0.0000076396450281944;1;57739400;DAB1;T;C;I_2321282;POEA;8;43212072;43212115
XX3762296;0.00334938423169702;1;31003973;LAPTM5;A;G;I_2321282;POEA;8;43212072;43212115

```

I get:

```nohighlight
$ sort -t\; -k2,2g infile346c
XX10914755;0.0879836449881873e-10;1;33854866;CSMD2;T;C;I_2321282;POEA;8;43212072;43212115
XX1547793;0.0000076396450281944;1;57739400;DAB1;T;C;I_2321282;POEA;8;43212072;43212115
XX12745618;0.00069063786440612;1;4019438;hCG_2036596;T;C;I_2321282;POEA;8;43212072;43212115
XX3762296;0.00334938423169702;1;31003973;LAPTM5;A;G;I_2321282;POEA;8;43212072;43212115

```

---

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 6:39pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/11 "2010-09-13T18:39:28Z")

</div>

No there is no error .. I copied also yours ..i do not know whats wrong .. is there any other function, which I can use for sorting ..I am so confused know ;(

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 13, 2010, 6:52pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/12 "2010-09-13T18:52:25Z")

</div>

I meant the - and the e were switched around in your example number: 0.0879836449881873-e10 should be 0.0879836449881873e-10 . What result did you get?

---------- Post updated at 00:52 ---------- Previous update was at 00:47 ----------

Is there a difference when you convert everything to scientific first? e.g.:

```nohighlight
awk 'BEGIN{FS=OFS=";"}{$2=sprintf("%e",$2)}1' infile | sort -t\; -k2.2g

```

---

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 6:59pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/13 "2010-09-13T18:59:04Z")

</div>

yes..I have seen it ..but the problem still remains,

```nohighlight
nils$ sort -t\; -k2,2g sort.txt 
XX10914755;0.0879836449881873e-10;1;33854866;CSMD2;T;C;I_2321282;POEA;8;43212072;43212115
XX12745618;0.00069063786440612;1;4019438;hCG_2036596;T;C;I_2321282;POEA;8;43212072;43212115
XX1547793;0.0000076396450281944;1;57739400;DAB1;T;C;I_2321282;POEA;8;43212072;43212115
XX3762296;0.00334938423169702;1;31003973;LAPTM5;A;G;I_2321282;POEA;8;43212072;43212115

```

I it sorts only for the first number before ".",e.g.

```nohighlight
nils$ sort -t\; -k2,2g sort.txt 
XX1547793;1.0000076396450281944;1;57739400;DAB1;T;C;I_2321282;POEA;8;43212072;43212115
XX10914755;2.0879836449881873e-10;1;33854866;CSMD2;T;C;I_2321282;POEA;8;43212072;43212115
XX3762296;3.00334938423169702;1;31003973;LAPTM5;A;G;I_2321282;POEA;8;43212072;43212115
XX12745618;5.00069063786440612;1;4019438;hCG_2036596;T;C;I_2321282;POEA;8;43212072;43212115

```

I do not know ..whats wrong

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 13, 2010, 7:09pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/14 "2010-09-13T19:09:02Z")

</div>

FWIW, I am getting:

```nohighlight
XX10914755;2.0879836449881873e-10;1;33854866;CSMD2;T;C;I_2321282;POEA;8;43212072;43212115
XX1547793;1.0000076396450281944;1;57739400;DAB1;T;C;I_2321282;POEA;8;43212072;43212115
XX3762296;3.00334938423169702;1;31003973;LAPTM5;A;G;I_2321282;POEA;8;43212072;43212115
XX12745618;5.00069063786440612;1;4019438;hCG_2036596;T;C;I_2321282;POEA;8;43212072;43212115

```

using sort (GNU coreutils) 7.4

---

<div class="post-metadata">

**Author:** ![NilsMueller](https://community.unix.com/letter_avatar/nilsmueller/32/5_5575768a8748004e209b776fc1b2916d.png) [@NilsMueller](https://community.unix.com/u/NilsMueller)\
**Post date:** [September 13, 2010, 8:00pm UTC](https://community.unix.com/t/sorting-exponential-notation/273258/15 "2010-09-13T20:00:42Z")

</div>

where do get the info which one I use??

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 14, 2010, 1:40am UTC](https://community.unix.com/t/sorting-exponential-notation/273258/16 "2010-09-14T01:40:12Z")

</div>

```nohighlight
sort --version

```

otherwise:

```nohighlight
man sort 

```
