3

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?

Cyrus
  • 84,225
  • 14
  • 89
  • 153

2 Answers2

3
sed -E -i "s/[0-9a-fA-F:]{17}/11:22:33:44:55:66/" file
Cyrus
  • 84,225
  • 14
  • 89
  • 153
2

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.

Community
  • 1
  • 1
rjt
  • 1,042
  • 10
  • 16