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

anl*_*lag 0 shell sort

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

示例输入数据:

$ cat test3.txt
Oct 10 14:00:00 andy@example.com bounced
Oct 10 13:00:00 andy@example.com deferred
Oct 10 14:30:00 billy@example.com bounced
Oct 10 12:00:00 andy@example.com deferred
Oct 9 12:00:00 cindy@example.com deferred
Oct 9 14:00:00 cindy@example.com bounced
Oct 10 12:30:00 billy@example.com deferred
Oct 10 13:30:00 billy@example.com deferred
Oct 9 13:00:00 cindy@example.com deferred
Run Code Online (Sandbox Code Playgroud)

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

$ sort -k 4,4 -k 1,1M -nk 2 test3.txt
Oct 9 12:00:00 cindy@example.com deferred
Oct 9 13:00:00 cindy@example.com deferred
Oct 9 14:00:00 cindy@example.com bounced
Oct 10 12:00:00 andy@example.com deferred
Oct 10 12:30:00 billy@example.com deferred
Oct 10 13:00:00 andy@example.com deferred
Oct 10 13:30:00 billy@example.com deferred
Oct 10 14:00:00 andy@example.com bounced
Oct 10 14:30:00 billy@example.com bounced
Run Code Online (Sandbox Code Playgroud)

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

我究竟做错了什么?

Fru*_*uit 6

如有疑问,请使用--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 cindy@example.com deferred
               ^ no match for key
___
    _
_________________________________________
Oct 9 13:00:00 cindy@example.com deferred
               ^ no match for key
___
    _
_________________________________________
Oct 9 14:00:00 cindy@example.com bounced
               ^ no match for key
___
    _
________________________________________
Run Code Online (Sandbox Code Playgroud)

这应该有效:

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 andy@example.com deferred
                ________________
___
    __
       __
_________________________________________
Oct 10 13:00:00 andy@example.com deferred
                ________________
___
    __
       __
_________________________________________

...

xb@dnxb:/tmp$ sort -b -k4,4 -k1M -k2n -k3n test3.txt
Oct 10 12:00:00 andy@example.com deferred
Oct 10 13:00:00 andy@example.com deferred
Oct 10 14:00:00 andy@example.com bounced
Oct 10 12:30:00 billy@example.com deferred
Oct 10 13:30:00 billy@example.com deferred
Oct 10 14:30:00 billy@example.com bounced
Oct 9 12:00:00 cindy@example.com deferred
Oct 9 13:00:00 cindy@example.com deferred
Oct 9 14:00:00 cindy@example.com bounced
xb@dnxb:/tmp$ 
Run Code Online (Sandbox Code Playgroud)

-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.
Run Code Online (Sandbox Code Playgroud)

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