13

I have a file with columns shown below:

chr1 91.4062 chr10 97.9150 chr11 116.7630 chr12 106.7870 chr13 116.1050 chr14 126.2180 chr15 110.2320 chr16 96.8076 chr17 113.5970 chr18 86.1011 chr19 130.6770 chr2 111.4620 chr20 68.4864 chr21 107.0810 chr22 140.7750 chr23 110.9590 chr24 68.4785 chr25 102.2080 chr26 72.2762 chr27 96.2213 chr28 85.5570 chr29 126.3800 chr3 116.1830 chr30 89.5663 chr31 89.1227 chr32 128.6190 chr4 117.3620 chr5 78.1921 chr6 85.4915 chr7 107.2620 chr8 112.9560 chr9 69.0250 chrX 66.0736 

I want to sort it based on 1st column and the output should look like below:

chr1 91.4062 chr2 111.4620 chr3 116.1830 chr4 117.3620 chr5 78.1921 chr6 85.4915 chr7 107.2620 chr8 112.9560 chr9 69.0250 chr10 97.9150 chr11 116.7630 chr12 106.7870 chr13 116.1050 chr14 126.2180 chr15 110.2320 chr16 96.8076 chr17 113.5970 chr18 86.1011 chr19 130.6770 chr20 68.4864 chr21 107.0810 chr22 140.7750 chr23 110.9590 chr24 68.4785 chr25 102.2080 chr26 72.2762 chr27 96.2213 chr28 85.5570 chr29 126.3800 chr30 89.5663 chr31 89.1227 chr32 128.6190 chrX 66.0736 

Any solution using linux commands would be helpful.

0

1 Answer 1

27

sort -V to the rescue:

sort -V file 

From man sort:

-V, --version-sort

natural sort of (version) numbers within text


In case you do not have the -V option in your sort command, there is an alternative: sort by first column starting on 4th character (-k1.4) and then sort numerically (-n).

sort -k1.4 -n file 

In both cases the output is as follows:

chrX 66.0736 chr1 91.4062 chr2 111.4620 chr3 116.1830 chr4 117.3620 ... chr26 72.2762 chr27 96.2213 chr28 85.5570 chr29 126.3800 chr30 89.5663 chr31 89.1227 chr32 128.6190 
Sign up to request clarification or add additional context in comments.

4 Comments

It says, sort: invalid option -- V
@user1779730 check my updated answer, there is an alternative.
It works. But it gives chrX in the begining which should be at the end.
@user1779730 if you want such behaviour, use a sort command that supports sort -v. As you can see, the alternative with -k and -n does put characters before digits.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.