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

查看:33
本文介绍了在 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_numeric 选项实现了 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天全站免登陆