2

Basically, I want to see how much time every user spent logged in.

(username) pts/0 (IP adress) Tue Dec 12 17:51 - 18:14 (00:22) - this is how one line looks in the last command. The username is between characters 1-9, the time is between 67-71. There are multiple logins from each user, so I want to sum the time based on their username. I already counted how many times one user logged in using

echo `last | sort | cut -c1-9 | uniq -c | sort -n | head -$y`

in a bash script. Is it possible to add up the time of each user using a format similar to this? I was thinking of a for loop, but have no ide how to set it up. Any ideas?

1

You could use the ac command for this. Part of the psacct package.

$ last|head
steve    pts/0        cpc79909-stkp12- Tue Nov 20 18:40   still logged in
steve    pts/0        cpc79909-stkp12- Mon Nov 19 22:19 - 23:09  (00:50)
steve    pts/0        cpc79909-stkp12- Wed Nov 14 19:36 - 19:45  (00:09)
steve    pts/0        cpc79909-stkp12- Fri Nov  9 11:43 - 11:56  (00:12)
steve    pts/0        cpc79909-stkp12- Thu Nov  8 21:58 - 22:00  (00:02)
steve    pts/0        cpc79909-stkp12- Mon Nov  5 17:37 - 18:30  (00:53)
steve    pts/0        cpc79909-stkp12- Fri Nov  2 19:45 - 20:39  (00:54)
steve    pts/1        cpc79909-stkp12- Fri Nov  2 17:34 - 18:31  (00:57)
steve    pts/0        cpc79909-stkp12- Fri Nov  2 16:01 - 18:30  (02:29)
steve    pts/0        cpc79909-stkp12- Thu Nov  1 21:10 - 23:14  (02:04)
$ ac -p
        steve                               33.79
        total       33.79
$

Or use awk ?

$ last | awk '$NF ~ /^\([0-9]+:[0-9]+\)$/{split($NF,t,":");a[$1]+=t[1]*60+t[2]}END{for(x in a){print x,a[x]}}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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