0

我有一个 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

有可能的?谢谢 :)

4

3 回答 3

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
于 2013-09-29T19:58:44.610 回答
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
于 2013-09-29T19:39:02.750 回答
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
于 2013-09-29T19:24:02.653 回答