Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I sort a text file with GNU sort starting with alphanumeric sort by one column, followed by numerical sort in the next column?

I tried sort -n -k1,2 file.txt, but it doesn't seem to work:

cluster-7402-walk-0270  100     136     SP3     0.959707
cluster-7402-walk-0270  113     137     SP1     0.959707
cluster-7402-walk-0270  117     136     SP2     0.959707
cluster-7402-walk-0270  117     136     SP4     0.959707
cluster-7402-walk-0270  118     140     SMAD4   0.984412
cluster-7402-walk-0270  121     144     ARNT    0.991913
cluster-7402-walk-0270  121     146     AHR     0.991913
cluster-7402-walk-0270  13      24      GTF2A1  0.965496
cluster-7402-walk-0270  13      24      GTF2A2  0.965496
share|improve this question

1 Answer 1

up vote 1 down vote accepted

For alphanumeric sorting you need to use version sort. The sorting type should be specified for each field, e.g. to sort alphanumerically on first column and numerically on next column use:

sort -k 1,1V -k 2,2n file.txt

See info sort and search for sort field specified for more on this.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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