Reputation: 849
The time tracking tool punch generates a timelog that includes a task and timestamps as in "yyyymmddThhmmss":
task started ended
---------------------------------------
task1 20121107T114348 20121107T120921
task2 20121107T121349 20121107T121430
task2 20121107T121658 20121107T124255
task1 20121107T140935 20121107T144153
task2 20121107T163449 20121107T180431
How could I sum the total hours and minutes spent on every task specified in a regex? E.g. to add up time spent on tasks that contain my-regex
, an awk
or gawk
command would be something like this:
awk '/my-regex/ { summing-of-corresponding-timestamps }' logfile.log
This is an addition to my previous question -- I realized I'm in need of a more "robust", regex-based solution. But I'm not a coder, so summing minutes and hours in AWK got me really confused. Thanks for any help!
Upvotes: 0
Views: 569
Reputation: 203655
try this with GNU awk, I haven't checked the math:
$ cat tst.awk
NR<3{ next }
{
start = mktime(gensub(/(....)(..)(..).(..)(..)(..)/,"\\1 \\2 \\3 \\4 \\5 \\6","",$2))
end = mktime(gensub(/(....)(..)(..).(..)(..)(..)/,"\\1 \\2 \\3 \\4 \\5 \\6","",$3))
dur[$1] += end - start
}
END {
for (task in dur)
print task, dur[task]
}
$
$ gawk -f tst.awk file
task1 3471
task2 6980
The outputs in seconds, massage to suit...
EDIT: to get the total duration of all tasks that satisfy some RE would be:
NR<3{ next }
$1 ~ re {
start = mktime(gensub(/(....)(..)(..).(..)(..)(..)/,"\\1 \\2 \\3 \\4 \\5 \\6","",$2))
end = mktime(gensub(/(....)(..)(..).(..)(..)(..)/,"\\1 \\2 \\3 \\4 \\5 \\6","",$3))
dur += end - start
}
END {
print dur
}
$ gawk -v re='<specify your RE>' -f tst.awk file
STEVE'S EDIT:
This answer is almost there. I've made a few small changes incorporating the information from the comments above/below. You can change the regex as required, for example:
awk '/task/ { a = "(....)(..)(..).(..)(..)(..)"; b = "\\1 \\2 \\3 \\4 \\5 \\6"; t += mktime(gensub(a, b, "", $NF)) - mktime(gensub(a, b, "", $(NF-1))) } END { print t }' file
Results:
10451
Upvotes: 5