0

这是我用来匹配变量中的字符串并删除匹配项所在的行的方法:

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
4

1 回答 1

0

如果前面的子字符串是固定的,您可以尝试以下操作:

PREFIX='cfg_file=\/etc\/nagios\/objects\/http_url\/'
DOMAIN='therecord.com'
sed -i "/^${PREFIX}${DOMAIN}/d" file

如果它不固定,最好使用负面的lookbehind,但sed不能这样做。您可以使用ssedGNU grep

ssed -Ri '/(?<!\.)'"$DOMAIN"'.cfg/d' file

或者

grep -vP '(?<!\.)'"$DOMAIN" > file1; mv file1 file
于 2013-01-08T18:52:39.850 回答