Karol Szustakowski
Karol Szustakowski

Reputation: 149

One command to count all sessions lasting more than an hour

I want to count all sessions longer than an hour, right now my solution looks like this:

last | rev  | cut -d" " -f1 | rev | tr -d "()" | egrep ":[0-9]{2}" | awk -F'[+:]' '{if(NF==3 || (NF==2 && $1 > 0)) print $0}' | wc -l

But I don't think it is really an elegant solution. Is there an other way to do this, using the last program itself?

Upvotes: 0

Views: 149

Answers (1)

tink
tink

Reputation: 15214

Try this one?

last | awk 'BEGIN{c=0}$NF~/[0-9]/{a=gensub(/\(([^:]+):.*/,"\\1",1,$NF);if(a!="00"){c++}}END{print c}'

Set a counter (c) to zero, for any line where the last field has a digit in it, split the last field at the colon, compare the left side against 00 (less than an hour), if they don't match, increment the counter; when all lines are processed, print the counter.

Upvotes: 1

Related Questions