?
Solved

Looking for a better ksh solution to format this string

Posted on 2004-10-06
3
Medium Priority
?
421 Views
Last Modified: 2013-12-26

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
Comment
Question by:scottzz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 12246231
It would be much quicker with sed

#!/bin/ksh
a="00112233445566778899AABBCCDDEEFF"
echo $a | sed -e "s/\(..\)/\1:/g" -e "s/:$//"
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 12266223
you can do all the magic using perl pack
http://www.perldoc.com/perl5.8.4/pod/func/pack.html
0
 

Author Comment

by:scottzz
ID: 12269154

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question