简体   繁体   English

在bash中对多个小数进行排序

[英]Sorting numbers with multiple decimals in bash

In bash using sort with the -n option doesn't give me the expected result. bash使用带-n选项的sort不会给我预期的结果。

$ cat numbers | sort -n
1.0
1.1
1.11.4
1.15
1.3
1.3.3
1.4-p1
1.6.1
2.2.10
2.2.2
2.4
2.4.6

I tried using -k1 , -k1.1n , etc. ( -k1.3n gets the order correct only for numbers starting with 1 ). 我尝试使用-k1-k1.1n等( -k1.3n仅对从1开始的数字获得正确的顺序)。 It seems there's something very basic I'm missing here... 看来这里有一些我非常基本的东西......

There is a special flag for this -V for version numbers 这个版本号有-V的特殊标志

$ sort -V numbers

1.0
1.1
1.3
1.3.3
1.4-p1
1.6.1
1.11.4
1.15
2.2.2
2.2.10
2.4
2.4.6

ps. PS。 this option is available in GNU Coreutils and may be missing in other implementations. 此选项在GNU Coreutils中可用,在其他实现中可能会丢失。

You need the -t. 你需要-t。 flag to specify '.' 用于指定'。'的标志 as your separator, and the multiple key position specifiers handles the progressively longer/deeper numbers. 作为您的分隔符,多个键位置说明符处理逐渐更长/更深的数字。 I still don't quite understand exactly how it works, but it works ... 我仍然不太确切地知道它是如何工作的,但是它有效......

 sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n numbers

or 要么

 cat numbers | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n
sort -g numbers

It will do. 它会的。 As per sort man page, -g is meant for numerical sorting: 根据排序手册页, -g用于数字排序:

-g, --general-numeric-sort -g, - general-numeric-sort

compare according to general numerical value 根据一般数值进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM