Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know that the command last | tac is enough, but I want to do it using the sort command. I cannot sort it by column, it always sorts the first one only. Using bash on Arch Linux.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Looks like you can't rely on fields, so you'd need to rely on character column

         1         2         3         4         5         6         7         
1234567890123456789012345678901234567890123456789012345678901234567890123456789
stephane pts/0        :0               Fri Aug  1 09:48 - 14:34 (17+04:45)
stephane pts/13       :0               Fri Aug  1 16:27 - 13:51 (20+21:24)

From that:

last | sort -k1.44,1.46M -k1.48,1.49n -k51

Note that the M flag to sort on month names is not standard but available in several sort implementations including GNU sort (the one typically found on ArchLinux). Note that sort interprets the month names in the current locale, while last always outputs English month names, so you may want to run sort under LC_TIME=C if in a non-English locale.

share|improve this answer
    
The M flag in sort (compare as months) is available on Sun OS/Solaris too. –  Dimitre Radoulov Aug 26 at 12:28
    
@DimitreRadoulov, indeed, it's even in HP/UX, so it's probably not a GNU extension as I thought it was. I've changed the wording. –  Stéphane Chazelas Aug 26 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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