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.txtOct 10 14:00:00 andy@example.com bouncedOct 10 13:00:00 andy@example.com deferredOct 10 14:30:00 billy@example.com bouncedOct 10 12:00:00 andy@example.com deferredOct 9 12:00:00 cindy@example.com deferredOct 9 14:00:00 cindy@example.com bouncedOct 10 12:30:00 billy@example.com deferredOct 10 13:30:00 billy@example.com deferredOct 9 13:00:00 cindy@example.com deferredThe 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.txtOct 9 12:00:00 cindy@example.com deferredOct 9 13:00:00 cindy@example.com deferredOct 9 14:00:00 cindy@example.com bouncedOct 10 12:00:00 andy@example.com deferredOct 10 12:30:00 billy@example.com deferredOct 10 13:00:00 andy@example.com deferredOct 10 13:30:00 billy@example.com deferredOct 10 14:00:00 andy@example.com bouncedOct 10 14:30:00 billy@example.com bouncedIf 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?
Using GNU sort with multiple keys, text and numbers
shell;sort
When in doubt, use --debug flag:xb@dnxb:/tmp$ sort -k 4,4 -k 1,1M -nk 2 test3.txt --debugsort: using en_SG.UTF-8 sorting rulessort: key 3 is numeric and spans multiple fieldsOct 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 --debugsort: using en_SG.UTF-8 sorting rulessort: key 3 is numeric and spans multiple fieldssort: key 4 is numeric and spans multiple fieldsOct 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.txtOct 10 12:00:00 andy@example.com deferredOct 10 13:00:00 andy@example.com deferredOct 10 14:00:00 andy@example.com bouncedOct 10 12:30:00 billy@example.com deferredOct 10 13:30:00 billy@example.com deferredOct 10 14:30:00 billy@example.com bouncedOct 9 12:00:00 cindy@example.com deferredOct 9 13:00:00 cindy@example.com deferredOct 9 14:00:00 cindy@example.com bouncedxb@dnxb:/tmp$ Your -nk 2 are wrong, as info sort stated:A position in a sort field specified with -k may have any of theoption letters MbdfghinRrV appended to it, in which case no globalordering options are inherited by that particular field.So option letters n should append to k and its position. The order matters.