Need to add colons


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to add colons
# 1  
Old 10-20-2006
Need to add colons

Hey guys i need to take the MAC address in the format of

0x00255T08D433

and convert it to

00:25:5T:08Smilie4:33 (it goes :08: D4:33 with no space the forum is making it a big smiley)

for a script i am working on. I am Not quite sure where to start with this one any help would be appreciated.

Sean
# 2  
Old 10-20-2006
Code:
a=`expr substr "0x00255T08D433" 3 2`

or
Code:
b=`echo "0x00255T08D433" | cut -c 3-4`

Code:
c=`expr substr "0x00255T08D433" 5 2
echo ${a}":"${b}

# 3  
Old 10-20-2006
Python alternative

Code:
a = "0x00255T08D433"
c = a[2:]            #c = "00255T08D433"
mac = [c[i:i+2] for i in range(0,len(c),2)]   ##mac = ['00', '25', '5T', '08', 'D4', '33']
print ':'.join(mac)

output:
Code:
00:25:5T:08:D4:33

# 4  
Old 10-23-2006
You can also use sed :
Code:
a=0x00255T08D433
b=$(echo "$a" | sed 's/^0[xX]//;s/\(..\)/\1:/g;s/:$//')
echo "$a --> $b"

Output :
Code:
0x00255T08D433 --> 00:25:5T:08:D4:33


Jean-Pierre.
# 5  
Old 10-23-2006
Sweet all of them work great. I went with aigles's code because it was the shortest but now I need to look into python. Thanks a lot guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove Colons and Slashes from 2nd Token

I would prefer to use Sed, but I will do whatever necessary. I want to know a good way to remove ":" and "/" from the first string surrounded in Double quotes, but not the 2nd. Current File: "9781238274584-Ace of Spades: The Poop" "Ace of Spades: The Poop" ...Desired: "9781238274584-Ace of... (6 Replies)
Discussion started by: glev2005
6 Replies

2. Shell Programming and Scripting

How to add add some files together and then paste

How can you efficiently add pairs of files together and then paste those pairs, I have a climate computer at work which spits out temperatures and stuff out twice a day, one for the day-data and one for the night (basically). Every week I want to make a nice overview The text files are... (1 Reply)
Discussion started by: uwgandalf
1 Replies

3. Shell Programming and Scripting

remove colons in MAC address

whats the simplest method to remove the colons from a mac address in PERL? thanks & regards (3 Replies)
Discussion started by: hazno
3 Replies

4. UNIX for Dummies Questions & Answers

Need Script to insert colons in each line of file

I have a file with over 500 MAC addresses. Each address is on a new line. However, the MACs do not have ":" I need a script that will read the file, line by line and insert colons in the addresses and then print the results to a new file. current.txt looks like this 111111111111 222222222222... (4 Replies)
Discussion started by: canopus15
4 Replies

5. UNIX for Dummies Questions & Answers

One or two colons with rsync?

I was reading about the different ways to run rsync. It looks like connecting to an rsync deamon is very similar to connecting to an rsync shell like ssh. Are there situations where the deamon is superior to the ssh? Are there situations where ssh is superior to the deamon? Thanks,... (0 Replies)
Discussion started by: siegfried
0 Replies
Login or Register to Ask a Question