Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

shell - How to find and replace MAC address

I have a file which has a data-

a AA:22:CC:44:DD:66 bbc aa:44:55:77:dd:rr axa
AA:22:CC:44:DD:66 sds 77:dd:rr:aa:44:55 dd 00:3a:7d:52:16:01 dd

My requirement is to find all the MAC addresses and replace it with XX:XX:XX:XX:XX:XX

So my final output should be-

a XX:XX:XX:XX:XX:XX bbc XX:XX:XX:XX:XX:XX axa
XX:XX:XX:XX:XX:XX sds XX:XX:XX:XX:XX:XX dd XX:XX:XX:XX:XX:XX dd

I have tried below, but it doesn't work fully

sed -r 's/(.*)([a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2})(.*)/1XX:XX:XX:XX:XX:XX3/g' file.txt
question from:https://stackoverflow.com/questions/65937940/how-to-find-and-replace-mac-address

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With your shown samples, try following. Written and tested with shown samples in GNU sed.

sed -E 's/([0-9a-eA-E]{2}:){5}[0-9a-eA-E]{2}/XX:XX:XX:XX:XX:XX/g' Input_file

Explanation: Simply using -E option of sed which enables ERE for regex. Then using s(substitution option) to substitute ([0-9a-eA-E]{2}:){5} digits, alphabets(small OR capital letters) 2 in number followed by a colon, then this combination should be there 5 times{5} and then again matching alphabets(small, capital letters) and digits 2 occurrences if match is found of this regex substitute them with XX:XX:XX:XX:XX:XX. using g option to globally substitute all matched/found matched ones with new value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...