user2053184
user2053184

Reputation:

Display users on Linux with tabbed output

I am working with Linux and I am trying to display and count the users on the system. I am currently using who -q, which gives me a count and the users but I am trying not to list one person more than once with it. At the same time I would like the output of users on separate lines as well or tabbed better than it currently is.

Upvotes: 0

Views: 72

Answers (2)

imp25
imp25

Reputation: 2357

The following will show the number of unique users logged in, ignoring the number of times they are each logged in individually:

who | awk '{ print $1; }' | sort -u | awk '{print $1; u++} END{ print "users: " u}'

If the output of who | awk '{ print $1 }' is :

joe
bunty
will
sally
will
bunty

Then the one-liner will output:

bunty
joe
sally
will
users: 4

Previous answers have involved uniq (but this command only removes duplicates if they are storted, which who does not guarantee, hence we use sort -u to achieve the same.

The awk command at the end outputs the results whilst counting the number of unique users and outputtig this value at the end.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201409

I think you want

who | awk '{print $1}' | uniq && who -q | grep "\# " | cut -d' ' -f2

Upvotes: 0

Related Questions