1

I have a long csv file containing 2 columns in the following format

RG_cne_1| 0.316571
RG_cne_10| 0.26924
RG_cne_100| 0.227538
RG_cne_1000| 0.385216
RG_cne_1001| 0.247508
RG_cne_1002| 0.496622
RG_cne_1003| 0.445685
RG_cne_1004| 0.53264
RG_cne_1005| 0.433687
RG_cne_1006| 0.295209
RG_cne_1007| 0.432938
RG_cne_1008| 0.368191
RG_cne_1009| 0.40916
RG_cne_101| 0.291027
RG_cne_1010| 0.389481
RG_cne_1011| 0.401816

I need to sort (change sequence of the lines) the data according to the name of the first column from RG_cne_1 to the last file (RG_cne_1011) in the following format

RG_cne_1| its value
RG_cne_2| its value
RG_cne_3| its value
RG_cne_4| its value
RG_cne_5| its value
...
RG_cne_1011| its value

On my Mac I tried to use

LC_ALL=C sort -t'|' -k1,1g

that however produced incorrect sorting of the numbers (simular to shown above: 1, 10, 100 etc)

1 Answer 1

3

The generalized numeric sort (g) still requires that the sort field starts with a number, or it will fall back to using a string comparison.

The following assumes that we want to sort numerically on the 3rd _-delimited field instead:

$ sort -t '_' -k 3,3n file
RG_cne_1| 0.316571
RG_cne_10| 0.26924
RG_cne_100| 0.227538
RG_cne_101| 0.291027
RG_cne_1000| 0.385216
RG_cne_1001| 0.247508
RG_cne_1002| 0.496622
RG_cne_1003| 0.445685
RG_cne_1004| 0.53264
RG_cne_1005| 0.433687
RG_cne_1006| 0.295209
RG_cne_1007| 0.432938
RG_cne_1008| 0.368191
RG_cne_1009| 0.40916
RG_cne_1010| 0.389481
RG_cne_1011| 0.401816
1
  • Thank you! It works !! Oct 23, 2020 at 11:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .