Sorting user by last login an count

Experts need your help

i need to create a script which will give count of users logged between yesterday and today (Or if run exacle 12:00AM midnight, it will give count of of users logged on that day
here is how the data looks

Pappu, Paul|22-Feb-201|0|30-Jun-201
Aloor, Shiva|10-Apr-201|0|18-Aug-201
Singh, Anil|16-Mar-201|0|10-Nov-201
Sun, Henry|09-Apr-201|0|05-Sep-201
Proxy,ETS|10-Apr-201|0|08-Mar-201
Patel, Rishi|11-Feb-201|0|27-Jun-201
Dubey, Anand|16-Apr-201|0|13-Mar-201
Tiwari, Shirish|19-Feb-201|0|13-Dec-201
Vinayagam, Deiva|15-Feb-201|0|04-Jul-201
infodba_ie|28-Feb-201|0|02-Dec-201
Murugesan, Gokul|03-Apr-201|0|07-Dec-201
cntestuser|21-Mar-201|0|08-Nov-201
Gupta, Rajiv|02-Apr-201|0|27-Jun-201
Airmal, Shiva Kumar|16-Apr-201|0|09-Dec-201
Joshi, Deepak|16-Apr-201|0|17-Sep-201
Wang,Charles|10-Apr-201|0|08-Nov-201

i use awk to get last column

cat user.txt |awk -F"|" '{print $2}'`

now my output is

22-Feb-201
10-Apr-2012
16-Mar-2012
09-Apr-2012
10-Apr-2012
11-Feb-2012
16-Apr-2012
19-Feb-2012
15-Feb-2012
28-Feb-2012
03-Apr-2012
21-Mar-2012
02-Apr-2012
16-Apr-2012
16-Apr-2012
10-Apr-20122
24-Jan-2012
09-Mar-2012
16-Apr-2012
27-Feb-2012
21-Mar-2012
13-Feb-2012
03-Apr-2012
06-Mar-201

My challenge is
1- I need to sort based on month and date
2- Get a list of user logged (system time -1), mean if execute and 12-05 midnight it will give user logged yesterday
3. get the count of the rows in column 2

Please help

---------- Post updated at 05:39 AM ---------- Previous update was at 04:27 AM ----------

I was able to sort it now using sort -t '-' -k 3.1n,3.2 -k 2.1M,2.3 -k 1.1n,1.2

24-Jan-2012
11-Feb-2012
13-Feb-2012
15-Feb-2012
19-Feb-2012
22-Feb-2012
27-Feb-2012
28-Feb-2012
06-Mar-2012
09-Mar-2012
16-Mar-2012
21-Mar-2012
21-Mar-2012
02-Apr-2012
03-Apr-2012
03-Apr-2012
09-Apr-2012
10-Apr-2012
10-Apr-2012
10-Apr-2012
16-Apr-2012
16-Apr-2012
16-Apr-2012

If i get tip to get user count of yesterday's assuming i execute this 1 minute after midnight

Hi,

I am not quite sure I understand what you want. But if you just want to count the number of times 16-Apr-2012 appears you could do the following. From your list of dates.

echo "24-Jan-2012
11-Feb-2012
13-Feb-2012
15-Feb-2012
19-Feb-2012
22-Feb-2012
27-Feb-2012
28-Feb-2012
06-Mar-2012
09-Mar-2012
16-Mar-2012
21-Mar-2012
21-Mar-2012
02-Apr-2012
03-Apr-2012
03-Apr-2012
09-Apr-2012
10-Apr-2012
10-Apr-2012
10-Apr-2012
16-Apr-2012
16-Apr-2012
16-Apr-2012" | grep "$(date +%d-%b-%Y --date="1 day ago")" | wc -l 

This is the version of date that I used.

date --version
date (GNU coreutils) 8.5
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.

Hope that helps you with your script.

Hi i need to check system date and list the count of entries which are one day older than the system date
My system date is in this format

infodba-ie10ux014:/home/infodba/execute\n\r-> date
Tue Apr 17 17:05:16 GMT 2012

Hence if i execute a command it should give me count of APR 16 entries

Ok. So you will not use the list of dates that you got from awk?
If you pass the results thru grep it will give you 3. Which is a count of yesterdays dates?

So taking what you have done. (skipping the sort)

awk -F"|" '{print $2}' user.txt | grep "$(date +%d-%b-%Y --date="1 day ago")" | wc -l

(As you wait for the experts) :slight_smile:

Hi Thanks Ni2, still need help
here is input data after using awk in your code

22-Feb-2012
10-Apr-2012
16-Mar-2012
09-Apr-2012
17-Apr-2012
11-Feb-2012
16-Apr-2012
19-Feb-2012
15-Feb-2012
28-Feb-2012
03-Apr-2012
21-Mar-2012
02-Apr-2012
16-Apr-2012
10-Apr-2012
24-Jan-2012
09-Mar-2012
16-Apr-2012
27-Feb-2012
21-Mar-2012
13-Feb-2012
03-Apr-2012
06-Mar-2012

now when i use you code

awk -F"|" '{print $2}' lastlogin_user.txt | grep "$(date +%d-%b-%Y --date="1 day ago")" | wc -l

i get Zero count, even though i added one entry with Apr 17

infodba-ie10ux014:/home/infodba/execute\n\r-> awk -F"|" '{print $2}' lastlogin_user.txt | grep "$(date +%d-%b-%Y --date="1 day ago")" | wc -l
       0

I mean i want to get count of entries with yesterday's date, in my case there one "17-Apr-2012" so the output should be 1

Please confirm where i am wrong, my system date format is

Wed Apr 18 11:14:14 GMT 2012

What does this give you

date +%d-%b-%Y --date="1 day ago"

Use below command to get result for yesterdays and today users..

awk -F "|" '{print $2}' lastlogin_user.txt | grep -E "$(date +%d-%b-%Y --date="1 day ago")|$(date +%d-%b-%Y)"

Hello ni2 sorry for late reply, here is what i get

infodb-> date +%d-%b-%Y --date="1 day ago"
18-Apr-2012

Hello Pamu. i tried get a exception for -E

infodba-ie10ux014:/home/infodba/execute\n\r-> awk -F"|" '{print $2}' lastlogin_user.txt | grep -E "$(date +%d-%b-%Y --date="1 day ago")|$(date +%d-%b-%Y)"
grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .
infodba-ie10ux014:/home/infodba/execute\n\r->

You should get 17-Apr-2012 not 18-Apr-2012.

Here are some other options on date arithmetic.

http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

HTH

ni2

thank you, i got thoufgh the FAQ page, this is what i used

$ export DATE_STAMP=`TZ=CST+24 date +%d-%b-%Y`
$awk -F"|" '{print $2}' lastlogin_user.txt | grep $DATE_STAMP|wc -l

Stillw ondering why the frirt command is not working

awk -F"|" '{print $2}' lastlogin_user.txt | grep "$(date +%d-%b-%Y --date="1 day ago")" | wc -l

Thank you very much