Trying to get list of logged on users sorted


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to get list of logged on users sorted
# 1  
Old 07-26-2006
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

Code:
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?
# 2  
Old 07-26-2006
Bug Hi!

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

Cheers.
# 3  
Old 07-26-2006
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

Code:
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.
# 4  
Old 07-26-2006
I know it's not pretty but

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

should work.
# 5  
Old 07-26-2006
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?

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

# 6  
Old 07-26-2006
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:

Code:
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.
# 7  
Old 07-26-2006
one-liner

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


Last edited by phrazz; 07-26-2006 at 03:21 PM.. Reason: slight mod
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to show a list of currently logged in and logging out users?

Hi Guys! I am sure that this question might appeared previously, but I still don't know how to show a list of logged out users. Please help with this! Thanks in advance:) (5 Replies)
Discussion started by: saloliubliu
5 Replies

2. Shell Programming and Scripting

(RHEL, Bash) List users and check if they have logged on during the last 2 months

Hi everyone, At work we were told to check the list of users of an application server and delete all those that have left the company or don't need access to the application anymore. Here's what I came up with. Would you be as kind as to tell me your opinion and whether there is a faster / easier... (4 Replies)
Discussion started by: gacanepa
4 Replies

3. UNIX for Dummies Questions & Answers

How many users are logged in?

How do I find this out? I have a feeling its a simple command such as who, but I just don't know what it is. I've had a search on here but either I can't put it into the right search criteria or there isn't a topic on it. Thanks. EDIT: Delete this thread, as I posted it I noticed the... (0 Replies)
Discussion started by: chris_rabz
0 Replies

4. Programming

Get the list of logged in users

How can I get the list of logged in users in the system programmatically? I can get the list with 'who' or 'users' commands but I need to get the list programmatically... May someone help, please? Thanks in advance. (2 Replies)
Discussion started by: xyzt
2 Replies

5. Shell Programming and Scripting

Users Not Logged in

I have searched the forums but have not mangaed to quite find what im looking for. I have used to /etc/passwd command to present me a list of all users the who command to present all users currently logged on, but what i want to know is what command can i use to display users that are registered... (12 Replies)
Discussion started by: warlock129
12 Replies

6. UNIX for Dummies Questions & Answers

How to do a login time and date list of users never logged on?

Hello, I'm trying to do a list of user that never connected to a couple of servers. I want to do a diff between the servers lists, and print out only the users that never has logged on each server. Here my first step : SERVER01: # finger `egrep -v -e "^\s*#" /etc/passwd | awk '{ print $1 }' |... (4 Replies)
Discussion started by: gogol_bordello
4 Replies

7. Solaris

List all inactive users who has not logged on since last 90 days

I need actuall script which List all inactive users who has not logged on since last 90 days Thanks in advance. Di! (17 Replies)
Discussion started by: haridham
17 Replies

8. UNIX for Dummies Questions & Answers

List all inactive users who has not logged on since last 90 days

Hi, Can I get a script to list out all the users, who has not logged on since last 90 days. Last command in not working due due to /var/adm/wtmpx is more than 2 GB. Thanks in advance. Regards, Roni (10 Replies)
Discussion started by: manasranjanpand
10 Replies

9. Post Here to Contact Site Administrators and Moderators

logged out users

how to find out users who logged out within 5 minutes (1 Reply)
Discussion started by: roshni
1 Replies

10. Shell Programming and Scripting

how many users logged

in unix what is the syntax to find out how many users are currently logged in (4 Replies)
Discussion started by: trichyselva
4 Replies
Login or Register to Ask a Question