这是我用来匹配变量中的字符串并删除匹配所在的行:
sed -i '/'"$domainAndSuffix.cfg"'/d' /etc/file
我想知道如何匹配变量中的字符串,但是如果文件中的匹配项有。在它的左边相邻,它不会删除这一行并继续浏览文件,直到找到没有的匹配。
示例文件内容:
happy.domain.com
pappy.domain.com
domain.com
要匹配的字符串:
domain.com
所需的文件输出:
happy.domain.com
pappy.domain.com
* 修改: 实际文件内容:
cfg_file=/etc/nagios/objects/http_url/bob.ca.cfg
cfg_file=/etc/nagios/objects/http_url/therecord.com.cfg
cfg_file=/etc/nagios/objects/http_url/events.therecord.com.cfg
cfg_file=/etc/nagios/objects/http_url/read.therecord.com.cfg
cfg_file=/etc/nagios/objects/http_url/wheels.ca.cfg
cfg_file=/etc/nagios/objects/http_url/used-vehicle-search.autos.ca.msn.com.cfg
cfg_file=/etc/nagios/objects/http_url/womensweekendshow.com.cfg
cfg_file=/etc/nagios/objects/http_url/yorkregion.com.cfg
cfg_file=/etc/nagios/objects/http_url/yourclassifieds.ca.cfg
答案 0 :(得分:0)
如果前面的子字符串是固定的,您可以尝试以下方法:
PREFIX='cfg_file=\/etc\/nagios\/objects\/http_url\/'
DOMAIN='therecord.com'
sed -i "/^${PREFIX}${DOMAIN}/d" file
如果没有修复,最好使用negative lookbehind,但sed
不能这样做。您可以使用ssed
或GNU grep
:
ssed -Ri '/(?<!\.)'"$DOMAIN"'.cfg/d' file
或
grep -vP '(?<!\.)'"$DOMAIN" > file1; mv file1 file