在Bash中用数字对字符串进行排序 [英] Sorting strings with numbers in Bash

查看:184
本文介绍了在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆