我有这种格式的mac地址列表:
412010000018 412010000026 412010000034
我想要这个输出:
41:20:10:00:00:18 41:20:10:00:00:26 41:20:10:00:00:34
我试过这个,但没有用:
sed 's/([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1:\2:\3:\4/g' mac_list
我该怎么办?
解决方法
您必须使用正确的sed语法:
\{I\} matches exactly I sequences (I is a decimal integer; for portability,keep it between 0 and 255 inclusive). \(REGEXP\) Groups the inner REGEXP as a whole,this is used for back references.
这是一个覆盖前两个字段的示例命令
sed 's/^\([0-9A-Fa-f]\{2\}\)\([0-9A-Fa-f]\{2\}\).*$/\1:\2:/'
以下命令可以处理完整的MAC地址,并且易于阅读:
sed -e 's/^\([0-9A-Fa-f]\{2\}\)/\1_/' \ -e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \ -e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \ -e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \ -e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \ -e 's/_\([0-9A-Fa-f]\{2\}\)/:\1/'
根据@Qtax发布的全局替换的perl解决方案的想法,可以得到更短的解决方案:
sed -e 's/\([0-9A-Fa-f]\{2\}\)/\1:/g' -e 's/\(.*\):$/\1/'