按列排序 linux

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18309538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:35:02  来源:igfitidea点击:

sort by column linux

linuxsorting

提问by chas

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.

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

采纳答案by fedorqui 'SO stop harming'

sort -Vto the rescue:

sort -V救援:

sort -V file

From man sort:

来自man sort

-V, --version-sort

natural sort of (version) numbers within text

-V, --version-sort

文本中的自然排序(版本)编号



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

如果您-Vsort命令中没有该选项,则有一种替代方法:从第 4 个字符 ( -k1.4)开始按第一列排序,然后按数字排序 ( -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