本文介绍了如何在 Unix 中对字母数字字符串进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我有一个乱序的表名列表.我怎样才能以正确的逻辑顺序获得它们?
<块引用>$ cat list.txt
TAB1选项卡13选项卡11TAB19选项卡2选项卡3TAB16TAB17TAB18选项卡9选项卡10选项卡8选项卡12TAB20
<块引用>
$ cat list.txt |sort -n
TAB1选项卡10选项卡11选项卡12选项卡13TAB16TAB17TAB18TAB19选项卡2TAB20选项卡3选项卡8选项卡9
预期订单:
TAB1选项卡2选项卡3选项卡8选项卡9选项卡10选项卡11选项卡12选项卡13TAB16TAB17TAB18TAB19TAB20
任何 vim 捷径也可以,我不一定需要一个单独的实用程序.
解决方案你需要告诉它你的排序键从哪里开始:
sort -n -k1.4 list.txt
否则它会从头开始,无法将字符串转换为数字并回退到按字母顺序比较.
I have a list of table names, which are out of order. How can I get them in the correct logical order?
$ cat list.txt
TAB1
TAB13
TAB11
TAB19
TAB2
TAB3
TAB16
TAB17
TAB18
TAB9
TAB10
TAB8
TAB12
TAB20
$ cat list.txt | sort -n
TAB1
TAB10
TAB11
TAB12
TAB13
TAB16
TAB17
TAB18
TAB19
TAB2
TAB20
TAB3
TAB8
TAB9
Expected order:
TAB1
TAB2
TAB3
TAB8
TAB9
TAB10
TAB11
TAB12
TAB13
TAB16
TAB17
TAB18
TAB19
TAB20
Any vim short-cuts will also do, I do not necessarily need a separate utility for this.
解决方案You need to tell it where your sorting key starts:
sort -n -k1.4 list.txt
Otherwise it starts from the beginning, fails to convert a string to a number and falls back to alphabetical comparison.
这篇关于如何在 Unix 中对字母数字字符串进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论