如何在 Unix 中对字母数字字符串进行排序?

编程入门 行业动态 更新时间:2024-10-27 16:28:22
本文介绍了如何在 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:关键词]

本文发布于:2023-04-01 15:39:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/834255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中对   字符串   字母   数字   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!