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.
1 Answer
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.
-
The M flag in sort (compare as months) is available on Sun OS/Solaris too. Commented Aug 26, 2014 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. Commented Aug 26, 2014 at 12:33