0

I am trying to find for how long a user is using my system, there ate say 3 users A@x.com, B@x.com and C@x.com. I have session.log in which all records are present like at what time one has logged in and then logged out.

Using Grep and Awk command i am able to get result like

07:50:00:942    A@x.com  Login 
07:50:19:612    A@x.com  Login 
07:52:14:697    A@x.com  Logout 
08:12:41:591    A@x.com  Login 
08:53:43:841    A@x.com  Login 
10:12:51:003    A@x.com  Logout 
10:25:21:004    A@x.com  Logout 
10:42:05:184    A@x.com  Logout 
11:02:43:803    A@x.com  Logout 
11:29:05:302    A@x.com  Login 
11:42:42:003    A@x.com  Login 
11:48:03:155    A@x.com  Login 
12:30:07:523    A@x.com  Logout 

02:22:48:003    B@x.com  Login 
02:33:51:005    B@x.com  Logout 
02:34:57:003    B@x.com  Logout 
03:37:42:186    B@x.com  Logout 
06:50:54:785    B@x.com  Logout 
08:14:06:002    B@x.com  Login 
08:40:53:557    B@x.com  Login 
09:02:44:574    B@x.com  Logout 
09:30:44:078    B@x.com  Login 

11:49:23:289    C@x.com  Login 
11:50:31:721    C@x.com  Logout 
12:00:19:101    C@x.com  Logout 
13:00:45:003    C@x.com  Login 
13:39:04:873    C@x.com  Login 
16:14:06:004    C@x.com  Logout 
17:52:11:345    C@x.com  Logout 

This all in a result.log file.

Now i want to finally find that for how long A,B and C have used system.

Please Guide me how do i traverse in this file for extracting time column and also at the same time to find which time is for login and which for logout.

Please suggest!

For better understanding i am sharing my code which i have written for extracting results.log-

#! bin/bash


echo "Enter start date(DD)"
read d1
echo "Enter start month(MM)"
read m1
echo "Enter start year(YYYY)"
read y1

echo "Enter end date(DD)"
read d2
echo "Enter end month(MM)"
read m2
echo "Enter end year(YYYY)"
read y2

>results.log
>temp1.log
>temp2.log
>temp3.log
>temp4.log
>temp5.log
>temp6.log
>temp7.log
>temp8.log
>temp9.log
>temp10.log
>temp11.log
>temp12.log
>temp13.log
>temp14.log
>temp15.log
>temp16.log




grep "@x.com" ../../../../logs/session.log.$y1-$m1-$d1| awk '{print $1,"\t",$9,"\t",$10,"\t",$11,"\t",$12,$13}' >>results.log

while [ $m1 -le $m2 ]
do




 if [ $m1 -eq $m2 -a $d1 -eq $d2 ]
 then

                break
 fi





 if [ $d1 -ge 31 ]
        then

        d1=01
        m1=`expr $m1 + 1`


        grep "@x.com" ../../../logs/session.log.$y1-$m1-$d1| awk '{print $1,"\t",$9,"\t",$10,"\t",$11,"\t",$12,$13}' >>results.log

 fi


 if [ $d1 -lt 31  ]
 then

d1=`expr $d1 + 1`

         if [ $d1 -lt 10 ]
         then

         grep "@x.com" ../../../logs/session.log.$y1-$m1-0$d1| awk '{print $1,"\t",$9,"\t",$10,"\t",$11,"\t",$12,$13}' >>results.log

     else



grep "@x.com" ../../../logs/session.log.$y1-$m1-$d1| awk '{print $1,"\t",$9,"\t",$10,"\t",$11,"\t",$12,$13}' >>results.log

         fi

fi

done


echo "\033c"

grep "A@x.com" results.log >> temp1.log
grep -E 'Login.*succeeded|Logout' temp1.log >>temp2.log

grep "B@x.com" results.log >> temp3.log
grep -E 'Login.*succeeded|Logout' temp3.log>>temp4.log

grep "C@x.com" results.log >> temp5.log
grep -E 'Login.*succeeded|Logout' temp5.log>>temp6.log

grep "D@x.com" results.log >> temp7.log
grep -E 'Login.*succeeded|Logout' temp7.log>>temp8.log

grep "E@x.com" results.log >> temp9.log
grep -E 'Login.*succeeded|Logout' temp9.log>>temp10.log

grep "F@x.com" results.log >> temp11.log
grep -E 'Login.*succeeded|Logout' temp11.log>>temp12.log

grep "G@x.com" results.log >> temp13.log
grep -E 'Login.*succeeded|Logout' temp13.log>>temp14.log

grep "H@x.com" results.log >> temp15.log
grep -E 'Login.*succeeded|Logout' temp15.log>>temp16.log

printf "Result for A\n"
cat temp2.log
printf " Result for B\n"
cat temp4.log
printf "Result for C\n"
cat temp6.log
printf "Result for D\n"
cat temp8.log
printf "Result for E\n"
cat temp10.log
printf "Result for F\n"
cat temp12.log
printf "Result for G\n"
cat temp14.log
printf "Result for H\n"
cat temp16.log

And this is the result which i am getting-

Result for A
01:59:27:884     A@x.com       Login   succeeded       739c9f980b5f0148 3
08:22:16:130     A@x.com       Logout          739c9f980b5f0148        User logged
08:25:28:208     A@x.com       Login   succeeded       9337d5188e27c848 2
10:54:21:003     A@x.com       Logout          9337d5188e27c848        Session expired
03:01:58:597     A@x.com       Login   succeeded       440748892c66413a 3
10:28:24:252     A@x.com       Logout          440748892c66413a        User logged
07:35:03:776     A@x.com       Login   succeeded       782ca7dceee92bd0 3
10:55:07:319     A@x.com       Logout          782ca7dceee92bd0        User logged
02:09:37:144     A@x.com       Login   succeeded       66609beb3ec0cdce 7
02:35:17:297     A@x.com       Login   succeeded       ee5eaf715cb9b2fa 8
03:09:54:003     A@x.com       Logout          66609beb3ec0cdce        Session expired
05:40:09:138     A@x.com       Login   succeeded       7430a3cbe6f4f7d7 7
05:57:21:004     A@x.com       Logout          ee5eaf715cb9b2fa        Session expired
10:39:16:648     A@x.com       Logout          7430a3cbe6f4f7d7        User logged
02:34:59:509     A@x.com       Login   succeeded       17c53664a82f2524 4
04:56:51:003     A@x.com       Logout          17c53664a82f2524        Session expired
05:59:17:234     A@x.com       Login   succeeded       586b58513ce94082 3
13:49:51:003     A@x.com       Logout          586b58513ce94082        Session expired
01:58:57:922     A@x.com       Login   succeeded       f97f8d50d9a4bdb7 4
11:02:43:803     A@x.com       Logout          f97f8d50d9a4bdb7        User logged
03:37:42:186     A@x.com       Login   succeeded       240040d22f757e3b 3
02:31:00:004     A@x.com       Logout          240040d22f757e3b        Session expired
10:55:56:519     A@x.com       Login   succeeded       43f37419e944a023 3
18:38:27:003     A@x.com       Logout          43f37419e944a023        Session expired
10:58:35:899     A@x.com       Login   succeeded       2e3ec796b82e70cb 5
11:34:33:004     A@x.com       Login   succeeded       1608b1b7a2bb707e 6
11:58:48:004     A@x.com       Logout          2e3ec796b82e70cb        Session expired
13:42:27:004     A@x.com       Logout          1608b1b7a2bb707e        Session expired
14:39:57:434     A@x.com       Login   succeeded       6cd1d55871376233 5
16:05:27:004     A@x.com       Logout          6cd1d55871376233        Session expired
 Result for B
02:22:36:053     B@x.com       Logout          7046b34ec8c0e2a5        User logged
03:45:38:238     B@x.com       Login   succeeded       1fe38047eaaa3232 5
05:40:03:055     B@x.com       Login   succeeded       3e747e80a62a2aad 5
11:10:36:756     B@x.com       Logout          1fe38047eaaa3232        User logged
05:11:02:526     B@x.com       Login   succeeded       204f0a61467e780 3
06:28:38:230     B@x.com       Logout          3e747e80a62a2aad        User logged
06:28:56:987     B@x.com       Login   succeeded       50935493d872a37 3
07:18:14:022     B@x.com       Logout          204f0a61467e780         User logged
07:18:27:696     B@x.com       Login   succeeded       eefdf95d7abfd897 3
10:24:38:929     B@x.com       Logout          eefdf95d7abfd897        User logged
03:01:48:837     B@x.com       Login   succeeded       36d29628d9a27de5 4
10:56:19:634     B@x.com       Logout          36d29628d9a27de5        User logged
13:41:00:003     B@x.com       Logout          50935493d872a37         Session expired
18:09:07:798     B@x.com       Login   succeeded       1960d80d076d26ca 3
02:18:12:004     B@x.com       Logout          1960d80d076d26ca        Session expired
18:39:48:038     B@x.com       Login   succeeded       25c319e5f7d10d9d 3
02:13:24:376     B@x.com       Logout          25c319e5f7d10d9d        User logged
18:30:14:283     B@x.com       Login   succeeded       586b952bd1b1e3 4
18:04:55:438     B@x.com       Logout          586b952bd1b1e3          User logged
18:11:21:764     B@x.com       Login   succeeded       45fd04c640564925 4
19:36:00:004     B@x.com       Logout          45fd04c640564925        Session expired
22:44:27:627     B@x.com       Login   succeeded       d6bb1b43888b80f6 2
22:48:48:736     B@x.com       Login   succeeded       4953500e78ba1371 3
01:57:58:241     B@x.com       Logout          4953500e78ba1371        User logged
15:01:42:003     B@x.com       Logout          d6bb1b43888b80f6        Session expired
18:59:02:989     B@x.com       Login   succeeded       7613a3ebc3aa78ae 2
02:22:05:090     B@x.com       Logout          7613a3ebc3aa78ae        User logged
Result for C
09:10:30:170     C@x.com       Login   succeeded       58bcc63ce89c490b 3
10:50:00:004     C@x.com       Logout          53d3c3e7141829f9        Session expired
11:01:48:004     C@x.com       Logout          58bcc63ce89c490b        Session expired
12:47:55:143     C@x.com       Login   succeeded       9181433fc8a7a253 4
17:54:11:379     C@x.com       Login   succeeded       7a3c94d6b0a2a0b5 5
18:42:38:317     C@x.com       Login   succeeded       324c019bc7a94144 4
18:49:21:005     C@x.com       Logout          9181433fc8a7a253        Session expired
02:57:54:003     C@x.com       Logout          324c019bc7a94144        Session expired
18:11:20:616     C@x.com       Login   succeeded       410f9337dc6f9678 8
19:34:11:865     C@x.com       Login   succeeded       105c78d30139f139 6
20:00:49:290     C@x.com       Login   succeeded       66f9f40463e45754 7
20:20:21:253     C@x.com       Login   succeeded       2f573db615e4e274 8
20:23:01:141     C@x.com       Login   succeeded       630c4ec47d4dd7ec 9
20:31:24:847     C@x.com       Login   succeeded       4821c9b5d28118 10
20:34:37:343     C@x.com       Logout          4821c9b5d28118          User logged
20:34:41:806     C@x.com       Login   succeeded       70726e38cf6daee3 10
20:36:57:950     C@x.com       Login   succeeded       b6f95ac0df08e55b 11
20:47:39:006     C@x.com       Logout          410f9337dc6f9678        Session expired
21:01:12:003     C@x.com       Logout          66f9f40463e45754        Session expired
21:20:48:004     C@x.com       Logout          2f573db615e4e274        Session expired
21:23:30:003     C@x.com       Logout          630c4ec47d4dd7ec        Session expired
22:44:57:004     C@x.com       Logout          70726e38cf6daee3        Session expired
23:33:27:153     C@x.com       Login   succeeded       b5a09f7771d45522 7
23:55:33:004     C@x.com       Logout          b6f95ac0df08e55b        Session expired
02:16:15:282     C@x.com       Logout          105c78d30139f139        User logged
14:09:00:004     C@x.com       Logout          b5a09f7771d45522        Session expired
4

1 回答 1

0

这是使用 Python 的解决方案:

import collections
import datetime
import sys

login = {} # { username : logintime }                                                                                   
state = collections.defaultdict(int) # { username : refcount }                                                          

def parse_time(text):
    """Return timedelta since midnight given input as hh:mm:ss:fff."""
    hh, mm, ss, fff = text.split(':')
    total_seconds = int(hh)*3600 + int(mm)*60 + int(ss) + int(fff)/1000
    return datetime.timedelta(seconds=total_seconds)

for line in open(sys.argv[1]):
    tokens = line.split()
    if tokens:
        time, user, action = tokens
        if action == 'Login':
            state[user] += 1
            if state[user] == 1:
                login[user] = time
        elif action == 'Logout' and state[user] > 0: # ignore redundant logouts                                         
            state[user] -= 1
            if state[user] == 0:
                duration = parse_time(time) - parse_time(login[user])
                print user, "logged in at", login[user], "for", duration

来自 OP 的样本输入的输出:

A@x.com logged in at 07:50:00:942 for 2:52:05
B@x.com logged in at 02:22:48:003 for 0:11:03
C@x.com logged in at 11:49:23:289 for 0:01:08
C@x.com logged in at 13:00:45:003 for 4:51:26
于 2013-08-08T11:51:56.133 回答