Capture the frequent username from the last command on unix - with exceptions

If I ran this on OS X:

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

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

How to use sed or awk to get the most common custom "chop"?

ADDITIONAL CHANGE TO QUESTION:
Our local administrator account hinders the results (username: support), and often we have a new starter in the client window.
How can i change

 last -1 

to omit the following and return the last valid username:

Support
reboot
shutdown

thanks

+4
source share
6 answers

In response to your change, leave the value of the numeric argument last and use egrep as follows:

 last | egrep -v 'support|reboot|shutdown' 

then pass this in awk , as in other answers.

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

sort -un is doing something, but I'm not sure if ...

 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 
+9
source

If you want to

  • a good ascending list of users with the number of logins (the material $0!~/^$/ only guarantees that empty lines will not be taken into account):

     last | awk '$0!~/^$/ {print $1}' | sort | uniq -c | sort 
  • username with the number of logins:

    add | tail -1 | tail -1 to the code above.

  • as stated above in awk (faster):

     last | awk '{a[$1]++}END{m=0;for(v in a){if(a[v]>m){m=a[v];u=v}}print m,u}' 
  • awk username only:

    remove the last m, from the above code.

+6
source

just awk

 last -10 |awk '{ user[$1]++} END{ t=0 for(i in user){ if (user[i]>t) { t=user[i] u=i } } print t,u }' 

with gawk, you can use asorti asorti internal functions

+2
source

The following pipeline offers a starting point, which I am sure can be optimized:

 last | awk '{A[$1] += 1; for (v in A) print A[v],v}' | sort -ur | head -n 1 | awk '{print $2}' 
+1
source

If you connect this to sorting, you will get an easily parsed list of data to find the most frequent user, although I don’t know how to find this user without a scripting language.

0
source

All Articles