Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grab frequent username from 'last' command in unix - with exceptions

Tags:

unix

macos

sed

awk

If I run this on OS X:

last -10 | awk '{print $1}'

I get: chop
chop
chop
chrihopk
reboot
shutdown
chop
chop
chop
chrihopk

How do I use sed or awk to obtain the most frequent user 'chop'?

ADDITIONAL EDIT TO QUESTION:
Our local admin account interferes with the results (username: support) and often we have a new starter on a client box.
How could I modify

last -1  

to omit the following and return the last valid username:

support
reboot
shutdown

Thanks

like image 839
chop Avatar asked Dec 02 '22 07:12

chop


1 Answers

bash$ last -10 | awk '{print $1}' | sort | uniq -c | sort -nr | head -1 | awk '{print $2}'

sort -un does something, but I'm not sure what...

bash$ echo -e 'bob\nbob\ncat\ncat\ncat\ndog'
bob
bob
cat
cat
cat
dog
bash$ echo -e 'bob\nbob\ncat\ncat\ncat\ndog' | sort | uniq -c | sort -nr
      3 cat
      2 bob
      1 dog
bash$ echo -e 'bob\nbob\ncat\ncat\ncat\ndog' | sort -un
bob
bash$ echo -e 'bob\nbob\ncat\ncat\ncat\ndog' | sort | uniq -c | sort -nr | head -n1 | awk '{print $2}'
cat
like image 142
Stobor Avatar answered Dec 26 '22 14:12

Stobor