按列排序

前端之家收集整理的这篇文章主要介绍了按列排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含如下列的文件
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

我想基于第1列对其进行排序,输出应如下所示:

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

任何使用linux命令的解决方案都会有帮助.

解决方法

排序-V救援:
sort -V file

从男人那种:

-V,–version-sort

natural sort of (version) numbers within text

如果您在sort命令中没有-V选项,则有另一种方法:从第4个字符(-k1.4)开始按第一列排序,然后按数字排序(-n).

sort -k1.4 -n file

在这两种情况下,输出如下:

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

猜你在找的Linux相关文章