How to analyze time tracking reports with awk?

一曲冷凌霜 提交于 2019-12-13 04:28:25

问题


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:


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"
    }
}


来源:https://stackoverflow.com/questions/13547138/how-to-analyze-time-tracking-reports-with-awk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!