[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

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
0
scottzz
Asked:
scottzz
1 Solution
 
TintinCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now