Update:
Here is a new implementation that makes use of dc
's "output base." Note that if the total sum is more than 60 hours, this will output four space-separated values instead of three. (And if the total sum is less than one hour, only two space-separated values will be output.)
awk '{print $2}' file.txt | tr : \ | dc -f - -e '60o0ddd[+r60*+r60d**+z1<a]dsaxp'
Input is assumed to be in triples of hour, minute, second, as shown in the question.
Output on the provided input is:
16 43
Original answer:
Let's do this with dc
your Desk Calculator. It's the back-end to bc
, and it's extremely flexible although often considered cryptic.
First, some pre-processing to give the times only, and convert the colons to spaces:
awk '{print $2}' | tr : ' '
We could also do this with Sed:
sed -En -e 's/^.*([0-9][0-9]):([0-9][0-9]):([0-9][0-9]).*$/\1 \2 \3/p'
I'll go with Awk and tr
because it's simpler. Either of the above command produces a clean output in the following format. (I'm using my own example text because I consider it more interesting; it has hours included. Yours will work as well.)
$ cat input
9 39 42
8 04 50
7 49 32
10 01 54
7 19 18
Given times in the above format, run them through the following Sed script and pipe the result into dc
as shown:
sed -e '1s/^/0 /' -e 's/$/ r 60 * + r 60 60 * * + +/' -e '$s/$/ 60 60 * ~ 60 ~ f/' input | dc
(Broken down to reduce sideways scrolling:)
sed <input \
-e '1s/^/0 /' \
-e 's/$/ r 60 * + r 60 60 * * + +/' \
-e '$s/$/ 60 60 * ~ 60 ~ f/' |
dc
The output will be seconds, minutes, hours, in that sequence. (Note this is a reversed sequence.) I'm just learning dc
so this isn't a perfect solution, but I think it's pretty good for a first look at dc
.
Example input and output, pasted directly from my terminal:
$ cat input
9 39 42
8 04 50
7 49 32
10 01 54
7 19 18
$ sed -e '1s/^/0 /' -e 's/$/ r 60 * + r 60 60 * * + +/' -e '$s/$/ 60 60 * ~ 60 ~ f/' input | dc
16
55
42
$