AvinashUpadhyaya
AvinashUpadhyaya

Reputation: 53

regex, sed command in unix for replacing MAC address in a file

I have a file that has multiple lines for different ethernet ports with their respective MAC addresses. I am trying to replace the mac address and the ethernet port name with my custom mac address using SED command. But I am not able to get the regex right with SED to replace mac address.

The file extract looks like:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:d9:00:ae", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

Can someone please me getting the sed command right for replacing mac address only 1 line at a time?

Upvotes: 3

Views: 6404

Answers (2)

rjt
rjt

Reputation: 1072

The original answer works great for the line of input given assuming there is nothing else in the line that would match that is not a MAC address. The following matches only six sets of 1 or 2 hexadecimal digits interspersed by a colon. Based on @robert-gamble answer https://stackoverflow.com/a/245925/711422

 sed -E -i "s/([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}/AA:22:CC:44:DD:66/" file

For example, given the following input android-db8f90123496fg97 AA:22:CC:44:DD:66, the original answer would also modify the android identifier.

Upvotes: 2

Cyrus
Cyrus

Reputation: 88756

sed -E -i "s/[0-9a-fA-F:]{17}/11:22:33:44:55:66/" file

Upvotes: 3

Related Questions