Trying to get list of logged on users sorted

I'm trying to execute a single shell command that will give me a sorted list of all the users currently logged into the system, displaying the users name as it appears in /etc/passwd.

I've tried

awk -F: '{print $1}' /etc/passwd | xargs finger -s | cut -c11-28 | uniq

This list whoever does not give me the current logged in users, since if I run who the list is much smaller.

Can anyone help me out, and give me some guidance here?

Kindly use the command "users" and sort it with the help of pipe.

Cheers.

That works to give me a list of usernames, but what I'm looking for is the users real names, sorted.

I have tried using

users | xargs grep /etc/passwd | cut -d: -f5 | sort -fd | uniq

But that does not work either since grep seems to try and execute the usernames instead of looking for them in the /etc/passwd file.

I know it's not pretty but

usrs=`users | sort`
for i in `echo $usrs`
do
grep $i /etc/passwd | awk -F \: '{print $1}'
done

should work.

That didn't really do what I wanted but I've modified it to give me the Real names at least, the problem still being that this list is not sorted, and this has to be executed from a command line.

I'm really lost here guys any help?

usrs=`who | cut -d" " -f1 | sort -df | uniq`
for i in $usrs
do
  grep $i /etc/passwd | awk -F \: '{print $5}'
done

it is sorted by user name not by real name. Put the sorting code in after you get the real names.

Try something like this:

if [ -f tmp.txt ]
then
  rm -f tmp.txt
fi
usrs=`users`
for i in $usrs
do
  grep $i /etc/passwd | awk -F \: '{print $5}' >> tmp.txt
done
sort tmp.txt 
rm tmp.txt

Again it's not pretty but it should work and sort by real name.

who | while read username junk ; do awk -F: '($1 == "'$username'") {print $5}' /etc/passwd ; done | sort -u

Thanks that worked perfectly