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 [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 billy@example.com deferred
Oct 9 13:00:00 [email protected] 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 [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 and[email protected] bounced
Oct 10 14:30:00 [email protected] 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?

1
  • Whats your desired output?
    – heemayl
    Oct 24, 2016 at 10:27

1 Answer 1

5

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

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 [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$ 

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
  • 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, 2016 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .