0

Suppose I have a input file that looks like this:

65CH6      N  990   3.717   2.836   2.649
65CH6   HMN1  991   3.807   2.880   2.647
61VAL      N  894   4.260   3.037   2.527
61VAL     HN  895   4.355   3.063   2.538
61VAL     CA  896   4.189   2.958   2.625
61VAL      C  908   4.057   3.023   2.669
61VAL      O  909   3.952   2.958   2.668
64PHE      N  938   3.817   3.002   2.420
64PHE     HN  939   3.915   3.018   2.412
64PHE     CA  940   3.760   2.868   2.403
64PHE     HA  941   3.682   2.866   2.327

Now I would like to sort these lines but based only on the first column, i.e. get something like this:

61VAL      N  894   4.260   3.037   2.527
61VAL     HN  895   4.355   3.063   2.538
61VAL     CA  896   4.189   2.958   2.625
61VAL      C  908   4.057   3.023   2.669
61VAL      O  909   3.952   2.958   2.668
64PHE      N  938   3.817   3.002   2.420
64PHE     HN  939   3.915   3.018   2.412
64PHE     CA  940   3.760   2.868   2.403
64PHE     HA  941   3.682   2.866   2.327
65CH6      N  990   3.717   2.836   2.649
65CH6   HMN1  991   3.807   2.880   2.647

instead of what I get with sort -g file.dat > file-sorted.dat

61VAL     CA  896   4.189   2.958   2.625
61VAL     HN  895   4.355   3.063   2.538
61VAL      C  908   4.057   3.023   2.669
61VAL      N  894   4.260   3.037   2.527
61VAL      O  909   3.952   2.958   2.668
64PHE     CA  940   3.760   2.868   2.403
64PHE     HA  941   3.682   2.866   2.327
64PHE     HN  939   3.915   3.018   2.412
64PHE      N  938   3.817   3.002   2.420
65CH6   HMN1  991   3.807   2.880   2.647
65CH6      N  990   3.717   2.836   2.649

And what I get with sort -g -k1,5 file.dat > file-sorted.dat is this

61VAL     CA  896   4.189   2.958   2.625
61VAL     HN  895   4.355   3.063   2.538
61VAL      C  908   4.057   3.023   2.669
61VAL      N  894   4.260   3.037   2.527
61VAL      O  909   3.952   2.958   2.668
64PHE     CA  940   3.760   2.868   2.403
64PHE     HA  941   3.682   2.866   2.327
64PHE     HN  939   3.915   3.018   2.412
64PHE      N  938   3.817   3.002   2.420
65CH6   HMN1  991   3.807   2.880   2.647
65CH6      N  990   3.717   2.836   2.649
user2300369
  • 299
  • 1
  • 3
  • 11

1 Answers1

1

You have to specify the starting key and number of keys, and also disable the "last resort" functionality that sorts everything else:

sort -g -k1,1 -s file.dat > file-sorted.dat

See the sort man page for more information.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351