How to analyze time tracking reports with awk?

2019-09-12 05:39发布

I'm tracking my time with two great tools, todotxt and punch. With these one can generate reports that look like this:

2012-11-23 (2 hours 56 minutes):
    first task (52 minutes)
    second task (2 hours 4 minutes)
2012-11-24 (2 hours 8 minutes):
    second task (2 hours 8 minutes)

My question: what's a convenient way for analyzing this kind of output? E.g. how could I sum up the time that is spent doing "first task"/"second task" or find out my total working hours for a longer period such as "2012-11-*"?

So, I'd like to have a command such as punch.sh report /regex-for-date-or-task-i'm-interested-in/.

I read and saw that this is possible with awk. But I don't know how to 1) sum minutes + hours and 2) provide "task names with spaces" as variables for awk.

UPDATE: I'm also tagging my tasks with +tags to mark different projects (as in first task +projecttag). So it would also be great to sum the time spent on all tasks with a certain tag.

Thanks for any help!

1条回答
在下西门庆
2楼-- · 2019-09-12 06:20

Before running this script. Please uncomment the appropriate gsub() function. Run like:

awk -f script.awk file

Contents of script.awk:

BEGIN {
    FS="[( \t)]"
}

/^\t/ {
    line = $0

#### If your input has...

##   No tags, show hrs in each task
#    gsub(/^[\t ]*| *\(.*/,"",line)

##   Tags, show hrs in each task
#    gsub(/^[\t ]*| *\+.*/,"",line)

##   Tags, show hrs in each tag
#    gsub(/^[^+]*\+| *\(.*/,"",line)

####

    for(i=1;i<=NF;i++) {
        if ($i == "hours") h[line]+=$(i-1)
        if ($i == "minutes") m[line]+=$(i-1)
    }
}

END {
    for (i in m) {
        while (m[i] >= 60) { m[i]-=60; h[i]++ }
        print i ":", (h[i] ? h[i] : "0") " hrs", m[i], "mins"
    }
}
查看更多
登录 后发表回答