BASH:拆分 MAC 地址 -> 000E0C7F6676 到 00:0E:0C:7F:66:76

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3265713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:20:13  来源:igfitidea点击:

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

macosbashstringsplit

提问by fwaechter

Hy,

嗨,

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

有人可以帮我从日志文件中拆分 mac 地址吗?:-)

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...

Atm 我用 OpenOffice 拆分了这个,但是有超过 200 个 MAC 地址'这很无聊而且很慢......

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

如果解决方案是在 bash 中,那就太好了。:-)

Thanks in advance.

提前致谢。

回答by falstro

A simple sed script ought to do it.

一个简单的 sed 脚本应该可以做到。

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.

这将获取 mac 地址列表myFile,每行一个,并在每两个十六进制数字后插入一个“:”,最后删除最后一个。

回答by ghostdog74

$ 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

回答by Fritz G. Mehner

Pure Bash. This snippet

纯猛击。这个片段

mac='000E0C7F6676'

array=()
for (( CNTR=0; CNTR<${#mac}; CNTR+=2 )); do
  array+=( ${mac:CNTR:2} )
done
IFS=':'
string="${array[*]}"
echo -e "$string"

prints

印刷

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

回答by Greg Bacon

$ perl -lne 'print join ":",  =~ /(..)/g while /\b([\da-f]{12})\b/ig' file.log
00:0E:0C:7F:66:76

If you prefer to save it as a program, use

如果您希望将其保存为程序,请使用

#! /usr/bin/perl -ln    
print join ":" =>  =~ /(..)/g
  while /\b([\da-f]{12})\b/ig;

Sample run:

示例运行:

$ ./macs file.log 
00:0E:0C:7F:66:76

回答by frankc

imo, regular expressions are the wrong tool for a fixed width string.

imo,正则表达式是固定宽度字符串的错误工具。

perl -alne 'print join(":",unpack("A2A2A2A2A2A2",$_))' filename

perl -alne 'print join(":",unpack("A2A2A2A2A2A2",$_))' filename

Alternatively,
gawk -v FIELDWIDTHS='2 2 2 2 2 2' -v OFS=':' '{$1=$1;print }'

或者,
gawk -v FIELDWIDTHS='2 2 2 2 2 2' -v OFS=':' '{$1=$1;print }'

That's a little funky with the assignment to change the behavior of print. Might be more clear to just print $1,$2,$3,$4,$5,$6

改变打印行为的任务有点奇怪。打印 $1,$2,$3,$4,$5,$6 可能更清楚

回答by Paused until further notice.

Requires Bash version >= 3.2

需要 Bash 版本 >= 3.2

#!/bin/bash
for i in {1..6}
do
    pattern+='([[:xdigit:]]{2})'
done

saveIFS=$IFS
IFS=':'
while read -r line
do
    [[ $line =~ $pattern ]]
    mac="${BASH_REMATCH[*]:1}"
    echo "$mac"
done < macfile.txt > newfile.txt
IFS=$saveIFS

If your file contains other information besides MAC addresses that you want to preserve, you'll need to modify the regex and possibly move the IFSmanipulation inside the loop.

如果您的文件除了要保留的 MAC 地址之外还包含其他信息,您将需要修改正则表达式并可能IFS在循环内移动操作。

Unfortunately, there's not an equivalent in Bash to sed 's/../&:/'using something like ${mac//??/??:/}.

不幸的是,在 Bash 中没有sed 's/../&:/'使用类似${mac//??/??:/}.