0

I have a file of numbers seperated by comma. I want to sort the list by columns 2 only. my expecation is that lines wil lbe sorted by only the second columns and not by additional columns. I am not looking to sort by multiple keys. I know how to sort by a single key. My question here is why when i am sotin giving start POS and end POS as column 2 why is it also sorting columns 3?

FILE

cat chris.num
1,5,2
1,4,3
1,4,1
1,7,2
1,7,1

AFTER SORT

sort -t',' -k2,2 chris.num
1,4,1
1,4,3
1,5,2
1,7,1
1,7,2

However my expected output is

1,4,3
1,4,1
1,5,2
1,7,2
1,7,1

So i thought since i give the start and end key as -k2,2 that it would sort only based on this column but it seems to be sorting the other columns too. how can i get this to sort only by column 2 and not by others

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • Possible duplicate of [Sorting multiple keys with Unix sort](https://stackoverflow.com/q/357560/608639), [Sort by third column leaving first and second column intact in Linux](https://stackoverflow.com/questions/11006431), etc. – jww Aug 08 '19 at 18:01
  • not sure why this is flagged as a duplicate or multiple keys when i clearly state single column. also I understand how to sort by columns, my point is it doesn't behave like i expect giving both a start andend key pos – Chris Doyle Aug 08 '19 at 18:07

1 Answers1

3

From the POSIX description of sort:

Except when the -u option is specified, lines that otherwise compare equal shall be ordered as if none of the options -d, -f, -i, -n, or -k were present (but with -r still in effect, if it was specified) and with all bytes in the lines significant to the comparison. The order in which lines that still compare equal are written is unspecified.

So in your case, when two lines have the same value in the second column and thus are equal, the entire lines are then compared to get the final ordering.

GNU sort (And possibly other implementations, but it's not mandated by POSIX) has the -s option for a stable sort where lines with keys that compare equal appear in the same order as in the original, which is what it appears you want:

$ sort -t, -s -k2,2n chris.num
1,4,3
1,4,1
1,5,2
1,7,2
1,7,1
Shawn
  • 47,241
  • 3
  • 26
  • 60