在Bash中用数字对字符串进行排序 [英] Sorting strings with numbers in Bash
问题描述
我经常想对带有数字的字符串进行排序,以便在排序时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.
是否有任何有效的方法来排序以获得所需的结果?查看每个字符,确定它是否为数字,然后从后续数字中构建一个子字符串,然后根据数字进行排序的想法太令人震惊了,无法在bash
中进行考虑.
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
选项实现了sort
的替代版本吗?
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常规数字排序
这篇关于在Bash中用数字对字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!