Solved

Looking for a better ksh solution to format this string

Posted on 2004-10-06
3
412 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
3 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to delete all specified values regedit 38 277
notReplace  challenge 53 116
centeredAverage challenge 8 131
Message not shown 5 33
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now