Sorting a comma separated file by the first column while preserving some order

1

I have a file of the form:

kl2,LL
kl2,nan
kl2,MORE
kl2,PRQ
kl2,JJ
abc87,PRQ
abc87,JJ
abc87,nan
abc87,MORE
abc87,LL
...

and I would like to sort this by the first column, while leaving the second column in the order that they currently are (for that block that correspond to the first column). The output I want would look like this:

abc87,PRQ
abc87,JJ
abc87,nan
abc87,MORE
abc87,LL
kl2,LL
kl2,nan
kl2,MORE
kl2,PRQ
kl2,JJ

I attempted to sort this like sort test.im -t, -k1 (and a few other variants) but every attempt has always sorted the second column too, and the output ends up looking like this:

abc87,JJ
abc87,LL
abc87,MORE
abc87,nan
abc87,PRQ
kl2,JJ
kl2,LL
kl2,MORE
kl2,nan
kl2,PRQ

anthr

Posted 2016-01-02T03:28:21.953

Reputation: 115

Answers

1

With GNU sort, add the --stable option. That "stabilize sort by disabling last-resort comparison". Also change -k1 to -k1,1.

sort test.im -t, -k1,1 --stable

glenn jackman

Posted 2016-01-02T03:28:21.953

Reputation: 18 546

Sorry - I probably should have mentioned that I've tried that (and just tested it again). I still see the second column as sorted alphabetically. – anthr – 2016-01-02T04:07:51.047

@anthr This is in the right direction. For your sample data, try it with the minor change -k1,1. For your real data, you may need as many 1s as there are actual columns. – Ouroborus – 2016-01-02T04:16:35.707

-k1,1 was the solution! Thanks a lot - I'll look now to try and understand why. – anthr – 2016-01-02T04:21:16.673