击:用数字字符串进行排序 [英] bash: sorting strings with numbers
问题描述
我经常要在它们与数字的字符串进行排序,以便,例如排序时 abc_2,abc_1,abc_10
的结果是 abc_1,abc_2,abc_10
。每样的机制我见过各种各样的 abc_1,abc_10,abc_2
,也就是从左边的字符的字符。
I've often wanted to sort strings with numbers in them so that, when sorting e.g. abc_2, abc_1, abc_10
the result is abc_1, abc_2, abc_10
. Every sort mechanism I've seen sorts as abc_1, abc_10, abc_2
, that is character by character from the left.
有没有排序,得到我想要的结果,任何有效的方法?看着每个字符,确定它是一个数字,建立一个子参与后续数字的和排序的想法,作为一个数字是太骇人在庆典
来考虑。
Is there any efficient way to sort to get the result I want? The idea of looking at every character, determining if it's a numeral, building a substring out of subsequent numerals and sorting on that as a number is too appalling to contemplate in bash
.
没有胡子的* nix大师落实的替代版本排序
与 - sensible_numerical
选项
Has no bearded *nix guru implemented an alternative version of sort
with a --sensible_numerical
option?
推荐答案
执行此
sort -t _ -k 2 -g data.file
- -t分隔符
- -k键/列
- -g一般数字排序
这篇关于击:用数字字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!