在MAC地址的字符之间添加符号

时间:2013-09-29 19:19:41

标签: sed awk grep

我有一个MAC地址列表,我需要每2个字符添加一个“:”。

我的意思是,如果我有这个:

23396 - FFFF00400001 - Lantastic
23397 - FFFF01E00004 - Lantastic
23379 - 0C000C0000 - ISL-Frame
23054 - F8D756 - Simm Tronic Limited
23055 - F8D7BF - REV Ritter GmbH

我需要解析这个:

23396 - FF:FF:00:40:00:01 - Lantastic
23397 - FF:FF:01:E0:00:04 - Lantastic
23379 - 0C:00:0C:00:00 - ISL-Frame
23054 - F8:D7:56 - Simm Tronic Limited
23055 - F8:D7:BF - REV Ritter GmbH

有可能吗?谢谢:))

3 个答案:

答案 0 :(得分:3)

$ awk '{gsub(/../,":&",$3);sub(/:/,"")}1' file
23396 - FF:FF:00:40:00:01 - Lantastic
23397 - FF:FF:01:E0:00:04 - Lantastic
23379 - 0C:00:0C:00:00 - ISL-Frame
23054 - F8:D7:56 - Simm Tronic Limited
23055 - F8:D7:BF - REV Ritter GmbH

答案 1 :(得分:2)

不完全短,但有效:

awk '{n=split($3,a,"");s="";for (i=1;i<=n-2;i+=2) s=s sprintf("%s%s:",a[i],a[i+1]);s=s a[n-1]a[n];$3=s}1' file
23396 - FF:FF:00:40:00:01 - Lantastic
23397 - FF:FF:01:E0:00:04 - Lantastic
23379 - 0C:00:0C:00:00 - ISL-Frame
23054 - F8:D7:56 - Simm Tronic Limited
23055 - F8:D7:BF - REV Ritter GmbH

答案 2 :(得分:1)

您可以使用替换命令:

perl -lane '$F[2] =~ s/(..)(?!$)/$1:/g; print qq|@F|' infile

它产生:

23396 - FF:FF:00:40:00:01 - Lantastic
23397 - FF:FF:01:E0:00:04 - Lantastic
23379 - 0C:00:0C:00:00 - ISL-Frame
23054 - F8:D7:56 - Simm Tronic Limited
23055 - F8:D7:BF - REV Ritter GmbH