My text contains a line like this:
DHCPD_INTERFACE="eth4"
There are other lines containing #DHCPD_INTERFACE=
I want to replace the whole line DHCPD_INTERFACE="eth4"
matching on DHCPD_INTERFACE=
but not if the line contains a #
I've tried: sed -i.bak '/^#/!s/DHCPD_INTERFACE=/DHCPD_INTERFACE="eth2"/g' dhcpd
But this doesn't replace the whole line.
This DHCPD_INTERFACE="eth4"
should be updated to DHCPD_INTERFACE="eth2"
but no line containing a #
and DHCPD_INTERFACE=
should be changed.
How can I do this.. ? Thanks
您可以使用anchor( ^
)来匹配行的开头:
sed 's/^\(DHCPD_INTERFACE="eth\)4"/\12"/' file
You can first replace the lines with a '#', then replace the lines with "eth4", and finally get back the ones with the '#'.
sed -i.bak s/#DHCPD_INTERFACE=/foo/g dhcpd
sed -i s/DHCPD_INTERFACE="eth4"/DHCPD_INTERFACE="eth2"/g dhcpd
sed -i s/foo/#DHCPD_INTERFACE=/g dhcpd
Not the best solution, but still a working one. Hope I helped !
sed solution:
The exemplary test
file contents:
DHCPD_INTERFACE="eth4"
#DHCPD_INTERFACE="eth4"
sed 's/\([^#]\|^\)DHCPD_INTERFACE="eth4"/\1DHCPD_INTERFACE="eth2"/g' test
The output:
DHCPD_INTERFACE="eth2"
#DHCPD_INTERFACE="eth4"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.