Looking for a better ksh solution to format this string


I have the following example that takes string "00112233445566778899AABBCCDDEEFF" and outputs "00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF"

Is there a better way to do this?

#!/bin/ksh

typeset -i i=1
typeset -i j=2
a="00112233445566778899AABBCCDDEEFF"

while ((i < ${#a}))
do
        b=$b`echo $a | cut -c$i,$j`

        i=i+2
        j=j+2

        if [ $i -lt ${#a} ]
        then
           b=$b":"
        fi

done

echo $b
scottzzAsked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
It would be much quicker with sed

#!/bin/ksh
a="00112233445566778899AABBCCDDEEFF"
echo $a | sed -e "s/\(..\)/\1:/g" -e "s/:$//"
0
 
HamdyHassanCommented:
you can do all the magic using perl pack
http://www.perldoc.com/perl5.8.4/pod/func/pack.html
0
 
scottzzAuthor Commented:

I was looking for a ksh solution, but thanks HamdyHassan.

Points and thanks go to Tintin, the answer is what I was looking for.
Thanks again!


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.