sed 在 mac 地址中插入冒号 [英] sed to insert colon in a mac address
本文介绍了sed 在 mac 地址中插入冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这种格式的 mac 地址列表:
I have a list of mac addresses in this format:
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
语法:
You have to use the correct sed
syntax:
\{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.
这是一个涵盖前 2 个字段的示例命令
Here is an example command that covers the first 2 fields
sed 's/^\([0-9A-Fa-f]\{2\}\)\([0-9A-Fa-f]\{2\}\).*$/\1:\2:/'
以下命令可以处理一个完整的MAC地址,并且易于阅读:
The following command can process a complete MAC address and is easy to read:
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 解决方案的想法,使用全局替换可以得到一个更短的解决方案:
Following the idea from a perl solution posted here by @Qtax with global substitutions one can get a shorter solution:
sed -e 's/\([0-9A-Fa-f]\{2\}\)/\1:/g' -e 's/\(.*\):$/\1/'
这篇关于sed 在 mac 地址中插入冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文