How to split numeric value into hours:minutes:seconds

I have a problem. I am working on a Call Detail Report system. Come to find out the phone switch does not report in seconds. It is a 5 digit field that reports h:mm:ss

The problem is I have 1-5 digit numbers
Ie 1 = 1 second and should be reported as 0:00:01
22 should be 0:00:22
321 should be 0:03:21
1334 should be 0:13:34
and 14322 should be reported as 1:43:22

Can anyone help me here? I appreciate it.

Hi,

Can you use 'perl'? Test this script. The last number is your input parameter (14322 in my example).

$ perl -Minteger -e '$a=$ARGV[0]; while ($a > 100) { push @time, $a % 100; $a /= 100; }; push @time, $a; push @time, "0" while @time < 3; printf "%01d:%02d:%02d\n", reverse @time;' 14322

Regards,
Birei

1 Like
$ echo 14322 |awk '{$0=sprintf ("%06d",$0);print $1$2,$3$4,$5$6}' FS= OFS=:
01:43:22

$ echo 22 |awk '{$0=sprintf ("%06d",$0);print $1$2,$3$4,$5$6}' FS= OFS=:
00:00:22
1 Like

Thank you everyone for the posts. Perl is great, but it is not "embraced" as much as awk.

Both examples work great!

Thank you for the help

sed 's/^/00000/;s/.*\(..\)\(..\)\(..\)$/\1:\2:\3/'
1 Like

Get idea from Scrutinizer's

printf %06d 123 |sed 's/\(..\)\(..\)\(..\)$/\1:\2:\3/'
1 Like