How to trim the output of the UNIX who Command?

Mike Needham picture Mike Needham · Feb 21, 2012 · Viewed 8.7k times · Source

I am working with an idea using the unix who command. As we all know, there does not seem to be a direct switch that gives just the username and line (terminal) info without the date and screen info... eg: the output is mneedham tty7...2012-02-19 11:26 (:0)

What I am trying to get is just the mneedham tty7 part. The solution needs to work no matter how long the username and terminal information.

I tried using tr -s ' ' (one space) like who | tr -s ' ' and that gave me one space between everything. Not quite what I was seeking. Tried cut -d" " -f1 gets the username only. So I am hopeful someone can help me find the right command to get both bits of information.

Thanks.

Answer

Andrew Clark picture Andrew Clark · Feb 21, 2012

Using cut:

who | cut -d " " -f1,2

Using awk:

who | awk '{ print $1, $2 }'