Solved

Looking for a better ksh solution to format this string

Posted on 2004-10-06
3
408 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
Comment Utility
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
Comment Utility
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
Comment Utility

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now