how to sed in this case

I want to change time format

For example ,

1:1:15 = Change to => 01:01:15
10:8:20 = Change to => 10:08:20
22:10:2 = Change to => 10:10:02

Thank in advance

sed 's/:/  /g;s/^/  /g;s/$/  /g;s/ \(.\) / 0\1 /g;s/  /:/g;s/^://;s/:$//' 
echo "3:1:12" | sed 's/:/  /g;s/^/  /g;s/$/  /g;s/ \(.\) / 0\1 /g;s/  /:/g;s/^://;s/:$//'

03:01:12

command should be much shorter with fprint

edit: printf not fprint :slight_smile:

echo "1:1:15" | awk -F: '{ printf("%02d:%02d:%02d\n", $1, $2, $3); }'

Thank u for all answer and it's work. :b::slight_smile:

PS , Could both of you to explain the code that u given?

Basically, in the awk script, all the work is done by the printf function. Awk is needed only to split the string in three separate variables, using the colon as delimiter.
I suggest you a "man printf" and "man formats", because this function is very powerful and can accept a large amount of string format specification. In our case, we need to print left padded numbers with zeroes, two characters as total length. You achieve this with the format string "%02d", where "d" represents an integer, "0" is the padding character and 2 is the length.