tags:

views:

435

answers:

6

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

A: 

If you pipe that into sort, you'll get an easily parsable list of data to find the most frequent user, although I don't know how to find that user without a scripting language.

Stefan Kendall
+8  A: 
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
Stobor
@earcar: You learn something new every day. Upon testing it, it seems to work... I'm confused about how it works, though, since I can find exactly zero documentation to say that it would do that... Is that a compatibility option from a million years ago?
Stobor
This definitely works for me too. Thanks for the help.
Chris Hopkins
@eacar: I knew about `sort -u`, and have used it where appropriate... Where is documented that `sort -un` behaves differently to `sort -n -u` ?
Stobor
too much pipe chaining and ugly.
ghostdog74
`sort -un` doesn't work for me on Mac OS X (GNU textutils 1.14)
Adam Rosenfield
@adam: I'm on osx 10.5 and it works: sort (GNU coreutils) 5.93 (no macports or similar)@stobor: it doesn't make any difference in behaviour here between `-u -n` and `-un`.. what version of sort do you have?
Carmine Paolino
@earcar: Yeah, you're right, I'm even more confused now. :-) `sort -n` is documented as sorting numerically. `sort -u` is documented as keeping only 1 copy of unique lines. How does that result in the combination of them outputting only 1 line?
Stobor
Also, it seems to print the wrong line in the face of quotation marks. :-S
Stobor
Hmm... Maybe it always fails?
Stobor
I'm now confused too.. It seems to me that sort -un displays the first line of input.. It's very strange and incorrect! Thanks for pointing this out!
Carmine Paolino
+1  A: 

The following pipeline offers a starting point, which I'm certain could be optimized:

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

If you want

  • a nice ascending-ordered list of users with number of logins (the $0!~/^$/ stuff only assures blank lines will not be counted):

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

    append | tail -1 to the code above.

  • same as 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}'
    
  • username only in awk:

    delete the last m, from the code above.

Carmine Paolino
+1 for `sort -un` even though I don't know where it says what that combination does...
Stobor
Perfect. The first option is exactly what I am after. Thanks a lot.
Chris Hopkins
edited because `sort -un` fails
Carmine Paolino
+2  A: 

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 make use of the asort, asorti internal functions

ghostdog74
A: 

In answer to your edit, leave off the numeric argument to last and use egrep like this:

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

then pipe that into awk as in the other answers.

Dennis Williamson