Adding columns of time

Hello all,

I'm in the process of writing a script, and I need to be able to add columns of time in the following format (time elapsed Net Backup logs):

000:01:03
000:00:58
000:00:49

Does anyone have a way of converting and/or adding timestamps such as these accurately?

Thanks in advance for any assistance.

convert to seconds, then revert the sum of seconds to time

awk -F ':'  '{sum+=($1*3600) + ($2*60) + $3 } 
                END{ a=int(sum/3600); sum-=(a*3600); b=int(sum/60); c=sum-(b*60)
                        printf("%03d:%02d:%02d\n", a, b, c) }

See how that works out.

also try:

awk -F: '
{ s+=$3; m+=$2; h+=$1; }
END {
  if (int(s/60)>0) {m+=int(s/60); s=s%60;}
  if (int(m/60)>0) {h+=int(m/60); m=m%60;}
  printf "%03d:%02d:%02d\n", h, m, s;
}
' file1
1 Like

Jim,
The solution you provided is not working for me.

rdrtx1, what you provided, I'm not sure if it is accurate or not. I pipe a command (rather than read a file) through what you provided. I am adding the numbers below, and get the following:

Four hours, fifty seven minutes, twenty seven seconds.

The backups for this particular host shows to have started at 04:00:00 and ended at 07:00:35.

004:57:27

Any thoughts? The elapsed times below is what was added...

HHH:MM:SS

000:00:17
000:00:23
000:00:24
000:00:26
000:00:27
000:00:32
000:00:35
000:00:38
000:00:40
000:00:45
000:00:46
000:00:47
000:00:47
000:00:49
000:00:49
000:00:50
000:00:50
000:00:50
000:00:51
000:00:52
000:00:53
000:00:53
000:00:55
000:00:55
000:00:58
000:00:58
000:01:00
000:01:00
000:01:01
000:01:03
000:01:04
000:01:06
000:01:06
000:01:06
000:01:08
000:01:08
000:01:09
000:01:10
000:01:10
000:01:12
000:01:12
000:01:12
000:01:13
000:01:13
000:01:14
000:01:15
000:01:15
000:01:17
000:01:18
000:01:18
000:01:21
000:01:22
000:01:24
000:01:24
000:01:33
000:01:44
000:02:02
000:02:07
000:02:19
000:02:20
000:02:33
000:03:49
000:04:05
000:04:10
000:04:20
000:05:24
000:05:57
000:08:17
001:00:35

Here is the code:

#!/bin/ksh

CLIENT3="server12"
TOTAL_CLIENT_ELAPSED=$(egrep -v "JobID" ${RPT_CSV} | egrep ${CLIENT3} | awk -F "," '{print $16}'| sort -n | awk -F: '
{ s+=$3; m+=$2; h+=$1; }
END {
  if (int(s/60)>0) {m+=int(s/60); s=s%60;}
  if (int(m/60)>0) {h+=int(m/60); m=m%60;}
  printf "%03d:%02d:%02d\n", h, m, s;
}')

CLIENT_START_TIME=$(egrep -v "JobID" ${RPT_CSV} | egrep ${CLIENT3} | awk -F "," '{print $13}' | sort -n | head -1)
CLIENT_END_TIME=$(egrep -v "JobID" ${RPT_CSV} | egrep ${CLIENT3} | awk -F "," '{print $15}' | sort -n | tail -1)

echo "Client StartTime EndTime TotalElapsed" | tr -s " " ","
echo "${CLIENT3} ${CLIENT_START_TIME} ${CLIENT_END_TIME} ${TOTAL_CLIENT_ELAPSED}" | tr -s " " ","

Output:

Client,StartTime,EndTime,TotalElapsed
server12,04:00:00,07:00:35,004:57:27

What is the output after the "sort -n"?

The same...

can you post the result for:

egrep -v "JobID" ${RPT_CSV} | egrep ${CLIENT3} | awk -F "," '{print $16}'| sort -n

Thanks again for your assistance,

It is the same as above:

000:00:17
000:00:23
000:00:24
000:00:26
000:00:27
000:00:32
000:00:35
000:00:38
000:00:40
000:00:45
000:00:46
000:00:47
000:00:47
000:00:49
000:00:49
000:00:50
000:00:50
000:00:50
000:00:51
000:00:52
000:00:53
000:00:53
000:00:55
000:00:55
000:00:58
000:00:58
000:01:00
000:01:00
000:01:01
000:01:03
000:01:04
000:01:06
000:01:06
000:01:06
000:01:08
000:01:08
000:01:09
000:01:10
000:01:10
000:01:12
000:01:12
000:01:12
000:01:13
000:01:13
000:01:14
000:01:15
000:01:15
000:01:17
000:01:18
000:01:18
000:01:21
000:01:22
000:01:24
000:01:24
000:01:33
000:01:44
000:02:02
000:02:07
000:02:19
000:02:20
000:02:33
000:03:49
000:04:05
000:04:10
000:04:20
000:05:24
000:05:57
000:08:17
001:00:35

Using that input with:

awk -F: '
{ s+=$3; m+=$2; h+=$1; }
END {
  if (int(s/60)>0) {m+=int(s/60); s=s%60;}
  if (int(m/60)>0) {h+=int(m/60); m=m%60;}
  printf "%03d:%02d:%02d\n", h, m, s;
}
'

result:

002:43:26

What do you get?

When I put the times into a file, and awked like your original example, I got the same results as you: 002:43:26

When I fed the times in on the fly, my result was different: 004:57:27

I want to get those same results without having to output to a file first. Investigating...

---------- Post updated at 02:45 PM ---------- Previous update was at 02:19 PM ----------

Looks like I had differing output because in a prior version of the script I narrowed the search by backup_client and Backup_Policy. Recently, I took out the specification for the backup policy, which added in a few almost hour-long incremental backup elapsed times, making the figures higher.