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
ID_5 0E0 436
ID_34 2E-69 2324
...

I wanna to sort the first column from smallest to largest first. After then, sort the exponential number from smallest to largest. At last, only sort the third column from largest to smallest.
All must sort at the same time running.

command I try:

sort -t_ -k2n -k2,2g -k3,3r input_file

But it seems like can't really work if I deal with huge data set.
Thanks for any advice.

$ sort -t' ' -k1.4n file
ID_1 0E0 3254
ID_1 0E0 5434
ID_1 0E0 6578
ID_1 1E-14 2524
ID_1 5E-52 46437
ID_3 65E-20 45467
ID_5 0E0 436
ID_34 2E-69 2324

Hi anbu23,

I hope that second column is from smallest to largest while third column is largest to smallest.

Thanks ya :slight_smile:

Combining both your proposals should do the job:

$ sort  -k1.4n -k2,2g -k3,3r 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
ID_5 0E0 436
ID_34 2E-69 2324
1 Like

Thanks RudiC.
It worked based on the input file.
Do you mind to explain what is the meaning of "sort -k1.4n" ?
I can't really understand it.

Thanks first :slight_smile:

man sort :

so it sorts using field 1 char 4 numerical as the first sort key. To be very precise, and avoid side effects, you would need to modify it to k1.4,1n

1 Like