Friday, May 27, 2011

Inserting colon after every 2nd digit using sed

Sometime while working with the MAC address from BRAS I need to convert the MAC address into colon delimited. Here is how to insert colon after every second digit

Contents of mac.txt

00e04dc540fe
00e04dc541f6
00e04dc54366
00e04dc5452e
00e04dc543f6
00e04dc5473a
00e04dc54852
00e04dc54a9e
c8d5fe1fd02c
c8d5fe1fa69c

The command:
$ sed 's/../&:/g; s/:$//' < mac.txt

Result:
00:e0:4d:c5:40:fe
00:e0:4d:c5:41:f6
00:e0:4d:c5:43:66
00:e0:4d:c5:45:2e
00:e0:4d:c5:43:f6
00:e0:4d:c5:47:3a
00:e0:4d:c5:48:52
00:e0:4d:c5:4a:9e
c8:d5:fe:1f:d0:2c
c8:d5:fe:1f:a6:9c

No comments:

Post a Comment