- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Sorting the last command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 09:51 AM
11-28-2000 09:51 AM
I am looking for a way to sort the output of the last command. For example, the following script will give me a list of the last time a user has logged on, but I need it sorted by the oldest first and the latest logins last.
It lists one entry per user and the script is all on one line with no carriage returns.
awk -F: '{print "last | grep "$1" | head -1"}' /etc/passwd|ksh - > /home1/nickd/last.log
Appreciate the assistance, Nickd
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 09:57 AM
11-28-2000 09:57 AM
Re: Sorting the last command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 10:03 AM
11-28-2000 10:03 AM
Re: Sorting the last command
The output looks like this:
smith ttyp4 Sat Nov 25 07:15 - 10:15 (03:15)
Sorting the Sat Nov 25 is the difficult part Rick.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 10:23 AM
11-28-2000 10:23 AM
Re: Sorting the last command
sort won't help you in this case, at least not on the 'raw' data.
I think that you'll need a small shell script which will convert "Nov" into "11".
Even then, I guess that the dates are shown like they are with 'll', I mean that hour:min is used for dates less than 6 months while year is shown for older dates.
I wrote such a program quite a while ago, I just need to put my hands on.
I'll try to find it for you.
Maybe that using the date package in perl would be easier....
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 10:31 AM
11-28-2000 10:31 AM
Re: Sorting the last command
Leslie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 10:45 AM
11-28-2000 10:45 AM
Re: Sorting the last command
I have tried sorting and the -M does not appear to sort the Month. Do you have specific examples?
Thanks,
Nickd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 11:12 AM
11-28-2000 11:12 AM
Re: Sorting the last command
Try this to order the output in ascending date sequence:
# last|sort -Mk4 -kn
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 11:13 AM
11-28-2000 11:13 AM
Re: Sorting the last command
Try this to order the output in ascending date sequence:
# last|sort -Mk4 -kn5
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 11:31 AM
11-28-2000 11:31 AM
Re: Sorting the last command
last | awk '{if (($1 = "nickd")) print $0}' | sort -Mk4 -kn5
This is working for me. Give thanks to JRF for providing the sort syntax.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 01:38 PM
11-28-2000 01:38 PM
Re: Sorting the last command
...because I favor great precision, let me amend my post a bit to cause the output to be sorted in ascending login time within ascending login date. Thus:
# last|sort -Mk4 -kn5 -kn6.1,6.2 -kn6.4
...adding Rick's filter for your total solution:
# last|awk '{if (($1=="nickd")) print $0}'|sort -Mk4 -kn5 -kn6.1,6.2 -kn6.4
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2000 03:10 PM
11-28-2000 03:10 PM
Re: Sorting the last command
man 1m runacct
man 1m lastlogin
Have runacct run daily and monacct run monthly. You will find all last logins recorded to the day in
/var/adm/acct/sum/loginlog
in the format
YY-MM-DD username
This doesn't help you if you want more specific times during the day.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 05:06 AM
11-29-2000 05:06 AM
Re: Sorting the last command
One final step is required.
The command/script:
last | awk '{if (($1 = "nickd")) print $0}' | sort -Mk4 -kn5
Works, but only with one userid, mine. I am terrible at awk and I was looking for a complete list of everyone that was listed in /etc/passwd.
Thanks all.
Nick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 05:33 AM
11-29-2000 05:33 AM
Re: Sorting the last command
With regard to your last wish, try this as a quick and brutal solution:
/usr/bin/sh
IFS=:
while read USER NULL
do
last|awk -v USER=$USER '{if (($1==USER)) print $0}'|sort -Mk4 -kn5 -kn6.1,6.2
-kn6.4
done < /etc/passwd
...is this that which you seek?...regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 05:42 AM
11-29-2000 05:42 AM
Re: Sorting the last command
Close big guy. This is the error that I get.
Let me ask you something that may help this. Can I have the output re-directed to a file first so that I can email it to myself automatically overnight when it runs?
The source line number is 1.
awk: Cannot find or open file -Mk4.
The source line number is 1.
awk: Cannot find or open file -Mk4.
The source line number is 1.
Thanks Nickd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 06:28 AM
11-29-2000 06:28 AM
Re: Sorting the last command
I should have just posted an attachment, so here it is with the addition of output redirection and automatic mailing to 'root'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 06:38 AM
11-29-2000 06:38 AM
Re: Sorting the last command
Oops! Use this version (attached). It adds redirection, mailing, and cleans up the previous log.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 08:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 11:39 AM
11-29-2000 11:39 AM
Re: Sorting the last command
Thanks for your help thus far.
One more point, I only want each user recorded once in my log, their most recent login.
Therefore, when I look at my log, it will show me the following:
james Thu Apr 20 08:00 - 08:10
nickd Fri Nov 17 09:00 - 17:00
Your report is very thorough and detailed, but one level of detail too deep, if you know what I mean.
Sorry to drag this out.
Nickd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 12:30 PM
11-29-2000 12:30 PM
Re: Sorting the last command
OK, now you can have it both ways -- run with the '-v' option for verbose output; no flags give most recent only!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2000 03:50 PM
11-29-2000 03:50 PM
Re: Sorting the last command
One thing you can do is to use the following to get your last output in reverse order is:
last | cat -n | sort -rn | cut -f 2
Apparently, what you want to know is the first time a user has logged in. I suggest that you take the script from James Ferguson and append the head -1 command to the end of the last $USER line.
One last thing: in unix, there many ways to do the same thing and you can do just about anything you except make a decent cup of coffee.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2000 10:52 AM
12-01-2000 10:52 AM
Re: Sorting the last command
Is it me, or does the latest procedure strictly go through the /etc/password file and list the last time each user login on?
I need one more sort, sorry to be a pain in the derriere, but sorted by last oldest date first and not by userid.
my email is Nickd63@yahoo.com if you need more details.
Thanks