This snippet uses a combination of Unix commands and a perl one-liner to format a report about what users have logged onto a server, how many times, and for how long total. I've added it to my crontab on one of my web servers, thinking that the information may come in handy someday.

Output looks like this:

wtmp begins Wed Aug 29 13:06 2001
...
                 rob:     4 logins    26.13 total time 
#!/usr/local/bin/bash last | tail -1; last | awk '{print $1}' | grep -v wtmp | sort | uniq - +c | perl -e 'while (<>) {my ($h, $w) = split; next unless $w; @l = sp +lit /\s+/, `ac -p $w`; printf "%20s: %5d logins %8s total time \n", $ +l[1], $h, $l[2]; }'

Replies are listed 'Best First'.
Re: Learning about users
by Hofmator (Curate) on Aug 31, 2001 at 13:27 UTC

    Know your command line switches ... and you can save a few keystrokes :) perl -ne 'my ($h, $w) = split; next unless $w; @l = split /\s+/, `ac -p $w`; printf "%20s: %5d logins %8s total time \n", $l[1], $h, $l[2];'

    Update: As CheeseLord remarked, you can additionally employ the -a switch leading to: perl -ane 'next unless $F[1]; @l = split /\s+/, `ac -p $F[1]`; printf "%20s: %5d logins %8s total time \n", $l[1], $F[0], $l[2];'

    Update2: Fixed an overlooked $w in my update, thanks CheeseLord for spotting that as well :)

    -- Hofmator