我有一些郵件日誌摘錄,我想先按電子郵件地址排序,然後按日期排序。
輸入資料範例:
$ 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
及其位置。順序很重要。