Nristen's (g)log
2021-12-07

Fun with Pipes

warning! very simple scripting to follow...

Today, when I was logged into my tilde server home, I suddenly wondered how many other people were logged in so I ran the command 'who' which produced a lot of output.

$ who
nristen  pts/72       2021-12-02 13:57 <ip address>
user1
user4
nristen
user2

Ok, I only want to see the usernames so pipe that output to awk which will only print the first column:

$ who | awk '{ print $1 }'
nristen
user1
user4
nristen
user2

Ok, that looks a little better however the output still has many duplicates (for people logged in more than once). I want to only show one entry for each user. For this, I will first sort the list and pipe the sorted output to the 'uniq' command which gets rid of the duplicate entries.

$ who | awk '{ print $1 }' | sort | uniq
nristen
user1
user2
user4

Awesome, now I have a list of users currently logged in. Now just out of curiosity, I wonder how many users are logged in? Counting by hand... never!

$ who | awk '{ print $1 }' | sort | uniq | wc -l
34

The command wc is short for word count and if I add the "-l" parameter, it counts the number of lines.

In the examples above, I did not include any actual usernames besides my own (nristen). The number of logged in unique users was actually 34 when I ran that command however.

--------

Nristen (g)log index

Nristen Home