Sort current logged in users by log in time (supposedly to be very easy but I'm missing something)


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Sort current logged in users by log in time (supposedly to be very easy but I'm missing something)
# 1  
Old 12-09-2012
Sort current logged in users by log in time (supposedly to be very easy but I'm missing something)

1. The problem statement, all variables and given/known data:

Show all users who are currently logged in, sorted from earliest to latest log in time. The log in time includes the month, day, and time.

2. Relevant commands, code, scripts, algorithms:

finger, who, sort, pipe, head, tail,

3. The attempts at a solution (include all code and scripts):
Code:
finger | tail -n +2 | sort -n +4 -5

I've also tried

Code:
finger | tail -n +2 | sort -M | sort +6 -7

(thinking that maybe column 7 is the time if you count Dec is 5th column, the 9th is 6th (see the output finger below)


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
De Anza College, Cupertino (California), US, Clare Nguyen, CIS 18A (It won't let me post link because I don't have 5 posts yet)

This is an introductory class to Unix.


Any help would be much appreciated.

Last edited by vtmd; 12-10-2012 at 12:14 PM..
# 2  
Old 12-09-2012
Here is an approach using awk that I wrote:-
Code:
finger |\
awk ' NR==1 {
  ind=index($0,"Login Time");
 } NR>1 {
  ldt=substr($0,ind,12);
  cmd="date -d \""ldt"\" +%s";
  while ((cmd | getline epoch ) > 0 ) {
   epoch=epoch;
  }
  close(cmd);
  printf "%s %s\n", epoch, $0;
} ' | sort -n | cut -c 12-

Actually I am converting the login time to epoch and performing the sort. You can try doing the same in shell script. I hope this helps.
# 3  
Old 12-09-2012
Hi bipinajith,
Thanks for the answer but it's too advance. Awk was not taught in this course Smilie
# 4  
Old 12-09-2012
Ok, here is a bash example. You might have to adjust the highlighted sub-string index and length according to your finger output.
Code:
#!/bin/bash

rm -f tmp

finger | while read line
do
        if [ $( echo $line | grep -c "^Login" ) -eq 0 ]
        then
                echo "$( date -d"${line:39:12}" +"%s" ) $line" >> tmp
        fi
done

sort -n tmp | cut -c 12-

# 5  
Old 12-10-2012
It's supposed to be just one single line command. We can use pipe. Can you think more simple? haha. I'm sorry but this is just an introductory class to Unix.
# 6  
Old 12-10-2012
How about w:
Code:
w | tail -n +3 | sort -k4n

# 7  
Old 12-10-2012
bipinajith,
We're almost there but the problem is w doesn't show the month and date.

Quote:
Show all users who are currently logged in, sorted from earliest to latest log in time. The log in time includes the month, day, and time.
finger does. Let's us start with something like this

Code:
finger | tail -n +2 | sort -n +4 -5

This is due tomorrow so if you can help me, I'd be greatly appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Log search and mail it if the log is updated before 24 hours from the current time

Hi , We have around 22 logs , each has different entries. I have to automate this using shell script. The ideas which am sharing is given below 1) We use only TAIL -100 <location and name of the log> Command to check the logs. 2) We want to check whether the log was updated before 24... (13 Replies)
Discussion started by: Kalaihari
13 Replies

2. Shell Programming and Scripting

Specified log in time for users

I have this task : Check the logintime.txt every minute to only allow user to log in at the specified time. logintime.txt has the following content: USER TIME_START TIME_STOP Example: john 17:00 18:00 My idea is locking the user at the TIME_STOP and unlocking at the TIME_START while... (4 Replies)
Discussion started by: muffle
4 Replies

3. Shell Programming and Scripting

current logged in user

Hey guys I need a script that reads a login name and verifies if that user is currently logged in i have found few commands like "who" and "users" but i wonder how can i verify it that login name is logged in or not? (3 Replies)
Discussion started by: nishrestha
3 Replies

4. OS X (Apple)

Guest missing from Log In Window but still Logged In

I have information on my Guest account on a webpage that is vital to my being able to leave to go home. I can leave as SOON as I recover this password, otherwise I'll have to wait a whole week :( Here's the thing: I entered login information on a site in the Guest Account. The computer has... (1 Reply)
Discussion started by: TurkeyBody
1 Replies

5. Shell Programming and Scripting

Get current logged in user from a script run as root.

Ok, so, in order to install some dependencies of a program I made, a script has to be run as root. The thing is that I have to copy some things into the home folder of currently logged in user, but the variable $HOME returns '/root' and the $USER returns 'root' :( Is there any way to see who is... (7 Replies)
Discussion started by: hakermania
7 Replies

6. Red Hat

Current logged in users

I have 2 systems. (1) RHEL5 and (2) winXP pro from xpPRO putty i ssh into rhel5 : user root from xpPRO i ftp into rhel5 : user abc123 when i run #uptime it only shows 1 user when i do #ps -u abc123 : it shows vsftpd deamon PID is there a command that can be used to show all currently... (4 Replies)
Discussion started by: dplinux
4 Replies

7. UNIX for Dummies Questions & Answers

Finding users logged on time and space useage

I need to find what users are currently logged onto the system that is easy just a simple who | awk '{ print $1 }' (thats all I need for the part), but I also need to find how long they have been logged on and the total amount of file space they are using. Thanks in advance, I have been looking... (3 Replies)
Discussion started by: mauler123
3 Replies

8. 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

9. Shell Programming and Scripting

shell script not getting current error messages with time from alert.log

Hi All, I need to get current error messages with time from alert.log.Below is my shell script but it's not working to meet this objective. could anyone pls share on the above issue for resolution: #################################################################### ## ckalertlog.sh ##... (2 Replies)
Discussion started by: a1_win
2 Replies

10. UNIX for Dummies Questions & Answers

Finding last time users logged in

I would like to find out the last time all users have logged in or out. I tried the last command, but it could not find the wtmp file in /var/adm (I searched in othe directories also). This is an AIX rs6000 4.2.1 system. We are moving our applications from this system to an AIX 5.2 system and I... (11 Replies)
Discussion started by: jyoung
11 Replies
Login or Register to Ask a Question