Back to the main page

My Tracking time (using AWK array)

When scripting, we all work with variables, whether system ones or our own ones. Basically a variable is name that holds some value, it's like small storage which content can change during script execution.

An array is also variable, but it stores a set of values and values are accessed by their indexes. With AWK, the index can be number or string.

So there is shell script, that uses awk array. This is simple example of tracking working time, how many hours are spent on different activities in a month. Of course, the script can be improved to calculate time per whole year or any other idea, but I'll leave this to you.

The scripts uses input data from text file mytime.txt. the file looks like:

# Legend:
# PRJ = Projects
# IUS = Internal Users Support
# EUS = External Users Support
# HWT = Hardware Troubleshoot
# SWT = Software Troubleshoot
# RD = Research and Development
# ------------------------------------------------------------------------------------------------------
#                                       Year 2010
# January:  Su M  Tu W  Th F  Sa M  Tu W  T  F  Sa Su M  Tu W  Th F  Sa Su M  Tu W  Th F  Sa Su M  Tu W
# Task/Day: 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# ------------------------------------------------------------------------------------------------------

PRJ         0  4  8  2  1  5  0  8  2  1  5  4  0  0  1  5  4  8  2  0  0  4  8  2  1  5  0  0  2  1  3
IUS         0  2  0  2  3  1  0  0  2  3  1  2  0  0  3  1  2  0  2  0  0  2  0  2  3  1  0  0  2  3  3
EUS         0  0  0  1  0  0  0  0  1  0  0  0  0  0  0  0  0  0  1  0  0  0  0  1  0  0  0  0  1  0  1
HWT         0  1  0  1  1  1  0  0  1  1  1  1  0  0  1  1  1  0  1  0  0  1  0  1  1  1  0  0  1  1  0
SWT         0  1  0  2  1  0  0  0  2  1  0  1  0  0  1  0  1  0  2  0  0  1  0  2  1  0  0  0  2  1  0
RD         0  0  0  0  2  1  0  0  0  2  1  0  0  0  2  1  0  0  0  0  0  0  0  0  2  1  0  0  0  2  1

The script is time.sh

#!/bin/sh
#set -x

# variables
FILE=mytime.txt

# sed removes comments and empty lines
# then pipe to (N)AWK section
cat ${FILE} | sed '/^#/d' | sed '/^$/d' | \

# start nawk section
nawk '

BEGIN {
print
print "Time spent this month: "
print "-=-=-=-=-=-=-=-=-=-=-=-="
}

# -- main start -----
{
total=0
# go from second to last field
for ( i=2; i<=NF; i++ )
        #  ... and add them to total
        total += $i

# Assign total hours of tasks to array tasktotal
# Number of Record (lines) increment, so it is index for array
tasktotal[NR] = total

# Assign first field (task name) to array taskname
taskname[NR] = $1

# print task name, total and hours
print $1, total, "hours"

}
# -- main end ------

END {
# add all total hours and find how much working hours/month
for (i=1; i<=NR; i++)
        monthtotal += tasktotal[i]

print "--------------------------------------"
print "Total work this month is " monthtotal " hours"
print "--------------------------------------"

# find working percentage of task for the month
for (i=1; i<=NR; i++)
        taskperc[i] = tasktotal[i] * 100 / monthtotal

print

# print task name and percentage of that task in the month
for (i=1; i<=NR; i++)
        print taskname[i] " : " taskperc[i] "%"

# just comment - how to print whole array
#printf "array is "
#for (i=1; i<=NR; i++)
#        printf  tasktotal[i] " "
#        print
}'
# nawk section finished

exit 0

So once file mytime.txt is filled with working hours, just run the script time.sh and the output is below.

# ./time.sh

Time spent this month:
-=-=-=-=-=-=-=-=-=-=-=-=
PRJ 86 hours
IUS 40 hours
EUS 6 hours
HWT 18 hours
SWT 19 hours
RD 15 hours
--------------------------------------
Total work this month is 184 hours
--------------------------------------

PRJ : 46.7391%
IUS : 21.7391%
EUS : 3.26087%
HWT : 9.78261%
SWT : 10.3261%
RD : 8.15217%
Back to the main page