Inconsistent behavior when sorting by column

I'm trying to sort the list below, so that multiple instances of the form
pass=i_d=j

are contiguous. Curiously, the standard sort command to do this works some of the time (it produced the output below) but not all of the time.

the command I used was

cat fileName | sort -k1.26,1.26n -k1.29,1.29n

where the contents of fileName are below.

this command produced 3 consecutive lines with pass=2_d=2 and 3 with pass=2_d=3, as intended, but then lower down the file, there are, for example 3 lines with pass=3_d=2, but separated by a line with pass=3_d=3.
That is, the ordering of the 3rd and 4th lines from the bottom should be reversed.

Could anybody please explain why I'm getting this inconsistent sort behavior?

Thanks very much for any help.

03/getBasisSet_n=03_pass=0_d=1_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=0_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=0_d=3_cov=0.050_dg=17_mthd=nIP.mat
09/getBasisSet_n=09_pass=0_d=4_cov=0.050_dg=17_mthd=nIP.mat
11/getBasisSet_n=11_pass=0_d=5_cov=0.050_dg=17_mthd=nIP.mat
04/getBasisSet_n=04_pass=1_d=1_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=1_d=2_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=1_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=1_d=3_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=1_d=3_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=2_d=1_cov=0.050_dg=17_mthd=nIP.mat
05/getBasisSet_n=05_pass=2_d=2_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=2_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=2_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=2_d=3_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=2_d=3_cov=0.050_dg=17_mthd=nIP.mat
09/getBasisSet_n=09_pass=2_d=3_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=3_d=1_cov=0.050_dg=17_mthd=nIP.mat
06/getBasisSet_n=06_pass=3_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=3_d=2_cov=0.050_dg=17_mthd=nIP.mat
07/getBasisSet_n=07_pass=3_d=3_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=3_d=2_cov=0.050_dg=17_mthd=nIP.mat
08/getBasisSet_n=08_pass=3_d=3_cov=0.050_dg=17_mthd=nIP.mat
09/getBasisSet_n=09_pass=3_d=3_cov=0.050_dg=17_mthd=nIP.mat

Presumably you wanted to sort on i and j in pass=i_d=j ; but the command:

cat fileName | sort -k1.26,1.26n -k1.29,1.29n

is sorting on i and the equal sign (and is slowing you down by reading and writing the contents of your input file an additional time).

Try:

sort -k1.26,1.26n -k1.30,1.30n fileName
1 Like

And, with one single letter to sort on, the n option doesn't hurt but is redundant.

1 Like

What a stupid mistake!
Thanks to RudiC and Don.