I have two input files. One has namelist, mm:dd form, duration of usage and other host name stuff in each line. The other one is the one that I generated that has a set of namel
I solved this using perl through this code from what I understood from you question.
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper; # $fields[3] => Month $fields[0] => user $fields[8] => Time
my $user_time = {};
my $time = {};
open my $READ , '<', 'test' or die;
while(my $line = <$READ>){
my @fields = split(' ', $line);
my $user = $fields[0];
my $month = $fields[3];
$fields[8] =~ m/([\d]*)\+?([\d]{2}):([\d]{2})/; #time format
my $min = $3;
my $hr = $2;
my $day = $1;
$day = 0 if (!$day);
if (!exists $user_time->{$month}->{$user}){
$time = {};
}
$time->{'day'} += $day*24;
$time->{'hr'} += $hr;
$time->{'min'} += $min;
$user_time->{$month}->{$user} = $time;
}
close $READ;
foreach my $month (keys %$user_time){
print "[$month]\n";
my $user_hash = $user_time->{$month};
foreach my $user (keys %$user_hash){
my $time = $user_hash->{$user};
print "$user\t". $time->{'day'}.'.'.$time->{'hr'}.'.'.$time->{'min'}."hours\n";
}
}
INPUT FILE:
sdou pts/11 Thu Dec 10 05:24 - 12:11 (2+06:46) 131.243.186.99
sdou pts/10 Thu Dec 10 05:04 - 12:11 (2+07:06) 131.243.186.99
sdou pts/9 Thu Dec 10 03:26 - 12:11 (2+08:45) 131.243.186.99
ermartin pts/0 Sat Dec 12 12:37 - 13:44 (01:06) c-24-130-14-154.hsd1.ca.comcast.net
ermartin pts/0 Sat Dec 12 12:18 - 12:31 (00:13) c-24-130-14-154.hsd1.ca.comcast.net
OUTPUT:
[Dec]
ermartin 0.1.19hours
sdou 144.21.97hours
Hope this helps.
I thought this was what you wanted (uses GNU awk 4.* for true multi-dimensional arrays):
$ cat tst.awk
{
n = split($9,t,/[()+:]/)
hours = t[n-3]*24 + t[n-2] + t[n-1]/60
tot[$4][$1] += hours
}
END {
for (month in tot) {
print "["month"]"
for (user in tot[month]) {
print user, tot[month][user] "hours"
}
}
}
$ awk -f tst.awk file
[Dec]
sdou 166.617hours
ermartin 1.31667hours
but the output numbers don't match your expected values:
[Dec]
sdou 94.xxxhours
ermartin 1.19hours
I've spent a lot of time trying to figure out why they're different but I can't. Sorry, hope this helps anyway.