# Inconsistent behavior when sorting by column

**URL:** <https://community.unix.com/t/inconsistent-behavior-when-sorting-by-column/348333>\
**Category:** Shell Programming and Scripting\
**Created:** [November 22, 2014, 5:16am UTC](https://community.unix.com/t/inconsistent-behavior-when-sorting-by-column/348333 "2014-11-22T05:16:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoKSimon](https://community.unix.com/letter_avatar/leoksimon/32/6_22734026db12803ebb3e059622ac3534.png) [@LeoKSimon](https://community.unix.com/u/LeoKSimon)\
**Post date:** [November 22, 2014, 5:16am UTC](https://community.unix.com/t/inconsistent-behavior-when-sorting-by-column/348333/1 "2014-11-22T05:16:30Z")

</div>

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

```nohighlight
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.

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [November 22, 2014, 5:46am UTC](https://community.unix.com/t/inconsistent-behavior-when-sorting-by-column/348333/2 "2014-11-22T05:46:52Z")

</div>

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

```nohighlight
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:

```nohighlight
sort -k1.26,1.26n -k1.30,1.30n fileName

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_22734026db12803ebb3e059622ac3534.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 22, 2014, 6:41am UTC](https://community.unix.com/t/inconsistent-behavior-when-sorting-by-column/348333/3 "2014-11-22T06:41:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LeoKSimon](https://community.unix.com/letter_avatar/leoksimon/32/6_22734026db12803ebb3e059622ac3534.png) [@LeoKSimon](https://community.unix.com/u/LeoKSimon)\
**Post date:** [November 22, 2014, 10:49am UTC](https://community.unix.com/t/inconsistent-behavior-when-sorting-by-column/348333/4 "2014-11-22T10:49:39Z")

</div>

What a stupid mistake!  
Thanks to RudiC and Don.
