Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: parsing output of UNIX `who` command

by Anonymous Monk
on Oct 02, 2006 at 09:21 UTC ( [id://575852]=note: print w/replies, xml ) Need Help??


in reply to parsing output of UNIX `who` command

Given that backticks in list context return a list of lines, and each line starts with the username (which you want to use as a key), I'd do something like:
my %who; foreach my $user (`who`) { $user =~ /^(\S+)/ or next; $who{$1}++; } while (my($user, $count) = each(%who)) { print "$user is logged in $count time"; print $count == 1 ? ".\n" : "s.\n"; }
who -q just gives the total number of users logged in on my system, so that's not very useful for counting how often each user is logged in.

Replies are listed 'Best First'.
Re^2: parsing output of UNIX `who` command
by johngg (Canon) on Oct 02, 2006 at 10:13 UTC
    On Solaris who -q gives one or more lines of space-separated user names followed by a summary line giving total no. of users. I wonder what o/s chinamox is using.

    Cheers,

    JohnGG

      Not on my Solaris.
      $ uname -a SunOS mybox 5.8 Generic_108528-15 sun4u sparc SUNW,UltraAX-i2 $ /usr/bin/who -q anomonk root # users=2 $
        Er ... yes.

        $ /usr/bin/who -q # You run 'who -q' and get ... anomonk root # One or more lines of space-separated user names # users=2 # Followed by a summary line $ # And back to the prompt

        If there are lots of users on a system, who -q will output multiple lines of users rather than wrapping one long line. It looks like it assumes a terminal width of 80.

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://575852]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found