2

I have a file in this format which I want to sort

RNU2-46P    0
RNU2-2P 9
RNU2-63P    5
RNU2-36P    6
RNU2-56P    8
RNU2-17P    0

I want to sort it on column 1 and get this output

RNU2-2P 9
RNU2-17P    0
RNU2-36P    6
RNU2-46P    0
RNU2-56P    8
RNU2-63P    5

I tried the sort command but it is not sorting the way I want. Help!!

Thanks

marked as duplicate by roaima, jasonwryan, Michael Homer, jimmij, Wildcard Jan 21 '16 at 2:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Does every line begin with RNU2-? – roaima Jan 21 '16 at 0:26
  • yes every line begins with RNU2- – user3138373 Jan 21 '16 at 0:27
1

Is this a good enough approximation?

cat garble
RNU2-46P    0
RNU2-2P 9
RNU2-63P    5
RNU2-36P    6
RNU2-56P    8
RNU2-17P    0



sort -t'-' -k2n garble
RNU2-2P 9
RNU2-17P    0
RNU2-36P    6
RNU2-46P    0
RNU2-56P    8
RNU2-63P    5
  • HI , this works pretty well, can you explain the 2n part?? – user3138373 Jan 21 '16 at 18:28
  • @user3138373 : Sure. With mixed strings like yours sorting numerically doesn't work. I told sort to consider '-' a field separator, and sort numerically on the 2nd field '-k2n'. Sort will read all numerical values of the field (discarding the P and everything after, since the P and spaces aren't digits). – tink Jan 21 '16 at 20:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.