Script to exchange place string with another string

1

I have the file /etc/udev/rules.d/70-persistent-net.rules that have such interfaces:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="1c:af:f7:e7:a4:3c", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="78:e3:b5:43:47:ad", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

I want to replace eth0 with eth1 and vice versa, tried to do with the sed command without success:

sed -i 's|eth0|eth1|g' 

It switches eth0 to eth1 obviously but now I have 2 eth1 interfaces.

I wanted a command that "switches" the place strings to get this result:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="1c:af:f7:e7:a4:3c", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="78:e3:b5:43:47:ad", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
    
asked by anonymous 21.07.2015 / 20:28

1 answer

3

You can not change A with B in one step. Must use a temporary value:

A  -> XX
B  -> A
XX -> B

In this case:

sed -e 's/eth0/ethXX/g' -e 's/eth1/eth0/g' -e 's/ethXX/eth1/g'

Return:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="1c:af:f7:e7:a4:3c", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="78:e3:b5:43:47:ad", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
    
22.07.2015 / 11:38