#!/bin/bash # # timesheet-sum - calculates total hours worked from a plaintext timesheet # timesheet must have daily totals in columns 25-29 =) DO_BACKUP=1 [ -f "$HOME/.timesheet-sum.conf" ] && source "$HOME/.timesheet-sum.conf" while [ "$1" ]; do # the timesheet file to sum target="$1" tmp="/tmp/`basename "$0"`-`basename "$1"`" backup="`dirname "$1"`/.`basename "$1"`~" # big ugly hack hours=$(echo 0 $(cat "$target" \ | grep -E '[[:digit:]]{2}:?[[:digit:]]{2}[[:space:]]*[[:digit:]]{2}:?[[:digit:]]{2}' \ | cut -c 25-29 \ | sed 's/^/+ /') \ | bc) # backup timsheet file if [ "$DO_BACKUP" ]; then cp "$target" "$backup" fi # remove any "Total" line(s) from target grep -v 'Total hours:' "$target" > "$tmp" && mv "$tmp" "$target" echo "Total: $hours hours" # update the total line in the timesheet echo -n Updating total info in file $target... echo " Total hours: $hours" >> "$target" && echo Done. shift done