0

I have this log file which is recording the log in/ log out sessions by users to the network

[10:21:10] User logged in 
[13:59:42] User logged out 
[15:42:00] User logged in 
[16:42:13] User logged out 
[11:15:02] User logged in 
[11:42:23] User logged out 

I want to calculate the total time spent on line for all users together, the log file above is actually very long and this is just an example.

in this example it will be 5:06:06 hours

2 Answers 2

1

You can try sed and awk for this task:

    cat file | sed -r 'N; s/\n/ /; s/] User logged [^ ]+//g; y/[:/  /' | awk '{s += ($4-$1)*3600 + ($5-$2)*60 + $6-$3}END {printf "%d:%02d:%02d\n", s/3600, s/60%60, s%60}'

    5:06:06

sed will line up "login" and "logout" time frames per user, then awk will do time calculation for the relevant columns.

1
  • 1
    This is brilliant!! Thank you very much !
    – Cellman
    Oct 7, 2018 at 15:23
0

If provided by your system, you could also try a combination of date, with the -f (file input) option reading from a "process substitution", to get at the seconds, and a simple awk summation:

date -f <(cut -c2-9 file1) +%s | awk '{getline X; SUM += X - $0} END {printf "%02d:%02d:%02d\n", SUM/3600, SUM/60%60, SUM%60}'
05:06:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .