使用具有多个键、文本和数字的 GNU 排序

使用具有多个键、文本和数字的 GNU 排序

我有一些邮件日志摘录,我想首先按电子邮件地址排序,然后按日期排序。

输入数据示例:

$ cat test3.txt
Oct 10 14:00:00 [email protected] bounced
Oct 10 13:00:00 [email protected] deferred
Oct 10 14:30:00 [email protected] bounced
Oct 10 12:00:00 [email protected] deferred
Oct 9 12:00:00 [email protected] deferred
Oct 9 14:00:00 [email protected] bounced
Oct 10 12:30:00 [email protected] deferred
Oct 10 13:30:00 [email protected] deferred
Oct 9 13:00:00 [email protected] deferred

当前版本中的文件以空格分隔。所以我想要的是首先按第四列排序,然后按第一列(如月份)、第二列(数字)和第三列(我猜是数字,除非时间戳需要特殊处理)排序。这是我最好的尝试:

$ sort -k 4,4 -k 1,1M -nk 2 test3.txt
Oct 9 12:00:00 [email protected] deferred
Oct 9 13:00:00 [email protected] deferred
Oct 9 14:00:00 [email protected] bounced
Oct 10 12:00:00 [email protected] deferred
Oct 10 12:30:00 [email protected] deferred
Oct 10 13:00:00 [email protected] deferred
Oct 10 13:30:00 [email protected] deferred
Oct 10 14:00:00 [email protected] bounced
Oct 10 14:30:00 [email protected] bounced

如果我只包含“-k 4,4”键参数,它会根据电子邮件进行排序,但当我添加其他键时,它似乎会被忽略。为简单起见,在此示例中可以忽略第一列;问题仍然存在,因为第二列的排序优先于第四列。

我究竟做错了什么?

答案1

如有疑问,请使用--debug标志:

xb@dnxb:/tmp$ sort -k 4,4 -k 1,1M -nk 2 test3.txt --debug
sort: using ‘en_SG.UTF-8’ sorting rules
sort: key 3 is numeric and spans multiple fields
Oct 9 12:00:00 [email protected] deferred
               ^ no match for key
___
    _
_________________________________________
Oct 9 13:00:00 [email protected] deferred
               ^ no match for key
___
    _
_________________________________________
Oct 9 14:00:00 [email protected] bounced
               ^ no match for key
___
    _
________________________________________

这应该有效:

xb@dnxb:/tmp$ sort -b -k4,4 -k1M -k2n -k3n test3.txt --debug
sort: using ‘en_SG.UTF-8’ sorting rules
sort: key 3 is numeric and spans multiple fields
sort: key 4 is numeric and spans multiple fields
Oct 10 12:00:00 [email protected] deferred
                ________________
___
    __
       __
_________________________________________
Oct 10 13:00:00 [email protected] deferred
                ________________
___
    __
       __
_________________________________________

...

xb@dnxb:/tmp$ sort -b -k4,4 -k1M -k2n -k3n test3.txt
Oct 10 12:00:00 [email protected] deferred
Oct 10 13:00:00 [email protected] deferred
Oct 10 14:00:00 [email protected] bounced
Oct 10 12:30:00 [email protected] deferred
Oct 10 13:30:00 [email protected] deferred
Oct 10 14:30:00 [email protected] bounced
Oct 9 12:00:00 [email protected] deferred
Oct 9 13:00:00 [email protected] deferred
Oct 9 14:00:00 [email protected] bounced
xb@dnxb:/tmp$ 

-nk 2错了,如上所述info sort

A position in a sort field specified with ‘-k’ may have any of the
option letters ‘MbdfghinRrV’ appended to it, in which case no global
ordering options are inherited by that particular field.

所以选项字母 n应附加到k及其位置。顺序很重要。

相关内容