如何在unix中首先用字母然后是数字对字母数字列进行排序 [英] How to sort alphanumeric column in unix with alphabet first and then numeric

查看:145
本文介绍了如何在unix中首先用字母然后是数字对字母数字列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文本文件:

Suppose I have a text file:

08174 C6517298
08184 P0785411
08184 K0255564
01234 56789012
09098 A9877756

我想在第二列中先按字母排序,然后按数字排序

I would like to sort the second column with alphabet first and then numeric

预期结果应为:

09098 A9877756
08174 C6517298
08184 K0255564
08184 P0785411
01234 56789012

我尝试了sort -gk2,2sort -k2,2,但都没有给我正确的结果.请帮忙.

I tried sort -gk2,2 and sort -k2,2, both do not give me the correct result. Please help.

推荐答案

-gk2,2替换为-k2g(您是说-k选项,对吗?),然后添加-k2

Replace -gk2,2 with -k2g (you meant the -k option, right?) , then add -k2

sort -k2g -k2 file

键定义语法为F[.C][OPTS][,F[.C][OPTS]],其中F是字段编号(尤其是2); OPTS是一个或多个单字母订购选项[bdfgiMhnRrV].

The key definition syntax is F[.C][OPTS][,F[.C][OPTS]], where F is the field number (2, in particular); OPTS is one or more single-letter ordering options [bdfgiMhnRrV].

请注意,您不需要,2,因为它意味着在第二列停止排序,而第二列是最后一列.

Note, you don't need the ,2, as it means to stop sorting at the second column, and the second column is the last.

关键选项优先级按照您在命令中传递优先级的顺序应用,即先应用-k2g,然后再应用-k2.

The key option priorities are applied in the order you passed them in the command, i.e. -k2g is applied first, then -k2.

这篇关于如何在unix中首先用字母然后是数字对字母数字列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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