Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: Split MAC Address -> 000E0C7F6676 to 00:0E:0C:7F:66:76

Hy,

Can someone help me with splitting mac addresses from a log file? :-)

This:

000E0C7F6676

should be:

00:0E:0C:7F:66:76

Atm i split this with OpenOffice but with over 200 MAC Address' this is very boring and slow...

It would be nice if the solution is in bash. :-)

Thanks in advance.

like image 621
fwaechter Avatar asked Dec 01 '22 05:12

fwaechter


2 Answers

A simple sed script ought to do it.

sed -e 's/[0-9A-F]\{2\}/&:/g' -e 's/:$//' myFile

That'll take a list of mac addresses in myFile, one per line, and insert a ':' after every two hex-digits, and finally remove the last one.

like image 108
falstro Avatar answered Dec 03 '22 18:12

falstro


$ mac=000E0C7F6676
$ s=${mac:0:2}
$ for((i=1;i<${#mac};i+=2)); do s=$s:${mac:$i:2}; done
$ echo $s
00:00:E0:C7:F6:67:6
like image 38
ghostdog74 Avatar answered Dec 03 '22 17:12

ghostdog74