1

I'm trying to sort my data by multiple keys with unix sort. I think that I get a wrong result. My command is

sort -t "_" -k4,4 -k2 -k1,1g  < stdev.txt

And the result:

0.322_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    0.000110687417806       0.0346076270248
0.3_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat      0.000111161259827       0.0358869210331
0.321_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    0.000134981044857       0.0457899948612
0.332_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    2.79712100925e-05       0.0049473335673
0.313_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    3.11625097814e-05       0.00588538959351
0.312_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    3.69066495111e-05       0.00819208397496
0.331_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    3.69774104969e-05       0.00824956236819
0.311_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    6.15395637079e-05       0.0173808578728
0.321_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat    0.000138353320007       1.05986015585
0.322_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat    0.00017460061705        0.521775402243
0.311_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat    0.000206502239096       0.149912367819
0.3_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat      0.000237775594814       0.633350656766
0.332_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat    3.1779126554e-05        0.0128586399133
0.313_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat    4.33297503265e-05       0.0166438194725
0.312_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat    7.21521358641e-05       0.0342760190842
0.331_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap4.dat    7.52883193115e-05       0.0416052108611
...
0.3_rsrc:8_phi:0.5_abr:2_prof:plaw_diff:point.dat       0.000124446390455       0.00132402479772
0.3_rsrc:8_phi:0.5_abr:2_prof:unif_diff:lap2.dat        1.2638050496e-05        0.0289450596111
0.3_rsrc:8_phi:0.5_abr:2_prof:unif_diff:lap4.dat        0.000100909900236       0.170116521056
0.3_rsrc:8_phi:0.5_abr:2_prof:unif_diff:point.dat       0.000237686616486       0.00142895807647
  • First key is read correctly (all abr:2s are at the end).
  • Second key is also read correctly (diff:lap2s are before diff:lap4s).
  • The last key -k1,1g is not read properly. According to the another SO question it should use only the first column (0.322, 0.3, etc.) with general numeric sort. Which is not performed (0.322>0.3 in lap2 sector) and unfortunately in lap4 sector the ordering is completely different. Command

    echo -e '0.3\n0.32\n0.28' | sort -g
    

    give correct result.

Is it possible to change field separator -t for each sorting key -k?

Luuuucky
  • 139
  • 1
  • 10
  • I forgot: `sort --version: sort (GNU coreutils) 8.23` – Luuuucky Nov 23 '17 at 12:33
  • Possible duplicate of https://stackoverflow.com/questions/19228968/unix-sort-n-t-gives-unexpected-result – tripleee Nov 23 '17 at 13:20
  • Possible duplicate of [unix sort -n -t"," gives unexpected result](https://stackoverflow.com/questions/19228968/unix-sort-n-t-gives-unexpected-result) – tripleee Nov 24 '17 at 05:56

1 Answers1

2

-k2 uses all the characters from the beginning of the 2nd field to the end of the line, because you did not specify where the key ends. So the lines

0.322_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat    0.000110687417806       0.0346076270248
0.3_rsrc:15_phi:0.5_abr:1_prof:gauss_diff:lap2.dat      0.000111161259827       0.0358869210331

are correctly sorted because in both keys begin with _rsrc:15 and 0.000110 sorts before 0.000111. The key phrase in the manual page is

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line's end.

AlexP
  • 4,370
  • 15
  • 15