views:

430

answers:

4

I have over 500 MAC addresses and I'm trying to find a simple way to insert colons between every 2 characters.

+1  A: 

If it's a one time thing, why not use emacs with a keyboard macro?

youtube link

epatel
+4  A: 

You could get notepad++ and do a search and replace with regex like search for (..) and replace with \1:

Matt
Why would '\1-' insert a colon?
Jonathan Leffler
Woops sorry should be \1:
Matt
A: 

It maybe overkill, but I would use Excel. Paste your MAC addresses into column A and this formula into column B:

=LEFT(A1,2)&":"&MID(A1,3,2)&":"&MID(A1,5,2)&":"&MID(A1,7,2)&":"&MID(A1,9,2)&":"&RIGHT(A1,2)

Then, you can copy column B, and either paste special...values into column C or just paste into Notepad.

Kevin Hakanson
+3  A: 

You could use a sed command such as this:

sed 's/\(\w\w\)\(\w\w\)\(\w\w\)\(\w\w\)\(\w\w\)\(\w\w\)/\1:\2:\3:\4:\5:\6/g' filename

This will just pull out 12 characters in groups of two, and spit them back out with colons in the middle. You could also try a simpler pattern like s/(\w\w)/\1:/g, though this will leave you with an extra colon at the end of every address.

Nick Lewis
Beat me to it! In re to your last sentance, you could then use another sed command to strip the trailing colon, and it might still be shorter.
TokenMacGuy
Not all versions of 'sed' support '\w' to mean a word-character. Greater precision in the match ('[0-9a-fA-F]') would make the iterative solution preferable.
Jonathan Leffler
I'd personally probably do it in Perl, but I have to upvote the decently simple Regex way of doing it.perl -e "while (<>) {$_ =~ s/(\w{2})/$1:/g; s/:$/$/;}".. I think that one would do it.
Sector Corrupt