Issue
I have a file /etc/config/network
config interface loopback
option ifname lo
option proto static
option ipaddr 127.0.0.1
option netmask 255.0.0.0
config interface lan
option ifname eth0
option type none
option proto static
option ipaddr 192.168.1.1
option netmask 255.255.255.0
config interface debug
option ifname usb0
option type none
option proto static
option ipaddr 172.18.0.18
option netmask 255.255.255.0
I want to rewrite it to be
config interface loopback
option ifname lo
option proto static
option ipaddr 127.0.0.1
option netmask 255.0.0.0
config interface lan
option ifname eth0
option type none
option proto dhcp
config interface debug
option ifname usb0
option type none
option proto static
option ipaddr 172.18.0.18
option netmask 255.255.255.0
I proceed this in this way:
- find
static
then delete two lines after(exclude linestatic
) - replace
static
withdhcp
Tried using sed '/static/{n;d}' /etc/config/network | sed -e 's/static/dhcp/' -e '/dhcp/{n;d}' > /etc/config/network
which is not so neat.
Could this be like sed -i -e <delete pattern> -e <replace pattern> /etc/config/network
?
Solution
Using GNU sed
$ sed -i.bak '/config interface lan/,/^$/{/static/{N;N;s/static.*/dhcp/}}' /etc/config/network
config interface loopback
option ifname lo
option proto static
option ipaddr 127.0.0.1
option netmask 255.0.0.0
config interface lan
option ifname eth0
option type none
option proto dhcp
config interface debug
option ifname usb0
option type none
option proto static
option ipaddr 172.18.0.18
option netmask 255.255.255.0
Answered By - HatLess Answer Checked By - Marie Seifert (WPSolving Admin)