0

I have some mail log excerpts that I'd like to sort first by e-mail address and then by date.

Example input data:

$ 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

The file in its current version is space delimited. So what I want is to sort first by the fourth column, and then by the first (as month), second (numerical) and third (numerical, I guess, unless the timestamps need special handling.) This is my best attempt:

$ 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

If I include only the "-k 4,4" key argument, it sorts fine according to the e-mail but that seems to get ignored when I add the other keys. For simplicity the first column can be ignored in this example; the problem is still there in that the sorting by the second column takes precedence over the fourth.

What am I doing wrong?

  • Whats your desired output? – heemayl Oct 24 '16 at 10:27
6

When in doubt, use --debug flag:

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
___
    _
________________________________________

This should works:

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$ 

Your -nk 2 are wrong, as info sort stated:

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.

So option letters n should append to k and its position. The order matters.

  • 1
    Thanks a lot, appending the n option to the second key argument, instead of prepending it, did indeed resolve my problem, so this is the correct answer. I neglected to mention versions; this is on a RHEL 6 system with sort 8.4, which unfortunately doesn't have the --debug option. I had studied the man page without finding the answer, but not the info page, which is quite a bit more informative. I will try to remember that in the future. – anlag Oct 26 '16 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.