删除由curl生成的文件中的行,该文件由my bash设置中的两个函数调用

删除由curl生成的文件中的行,该文件由my bash设置中的两个函数调用,bash,hosts,Bash,Hosts,我在Mac OS X上,使用called bycurl帮助生成我的主机文件。位于/etc/hosts.d/*的目录有几个文件: 1-warning // File of comments that explains how this whole thing works 2-hosts-original // The original OS X hosts file 3-adobe // Some Adobe hosts that flash talks to way too often 4-so

我在Mac OS X上,使用called by
curl
帮助生成我的主机文件。位于/etc/hosts.d/*的目录有几个文件:

1-warning // File of comments that explains how this whole thing works
2-hosts-original // The original OS X hosts file
3-adobe // Some Adobe hosts that flash talks to way too often
4-someonewhocares // The results of the `curl` command in my below function
5-development // Any local dev work I am doing to make my own quick domains
curl
运行时,会编写“4-someonewhocare”。我使用两个函数,因为主机
curl
调用可能会变慢、变慢、中断等

这两个功能:

function write-hosts() {
    cat /etc/hosts.d/* > /etc/hosts;
}

function update-hosts() {
    curl http://someonewhocares.org/hosts/zero/hosts -o /etc/hosts.d/4-someonewhocares 2> /dev/null
    write-hosts
}
每隔几天,我调用
updatehosts
,它会写入一个新的“4-someonewhocare”文件,完成后,函数
write hosts
cat
会将所有文件保存在/etc/hosts.d/*中,并将其输出到/etc/hosts

我想从“4-someonewhocares”文件中排除几行。例如:

www.googleadservices.com
feedads.googleadservices.com
我认为最好在
write hosts
函数中删除行,因为在
curl
完成之前,永远不会调用该函数。但是我也可以在
curl
命令之后和
write hosts
函数之前完成

我正在寻找关于从结果“4-someonewhocares”文件中删除行的最佳位置的建议,以及从文件中删除行的建议方法。理想情况下,我会在/etc/hosts.d中放置一个“6-whitelist”文件,但它会变得复杂,我不知道如何从
cat
命令中排除它,尽管我可以按名称列出每个文件,因为它们不会改变


感谢您的建议。

首先创建6-白名单:

$ cat /etc/hosts.d/6-whitelist
www.googleadservices.com
feedads.googleadservices.com
现在,修改
write hosts

write-hosts() { grep -vhFf /etc/hosts.d/6-whitelist /etc/hosts.d/* >etc/hosts; }
工作原理 修订后的
写入主机
/etc/hosts.d/*
复制所有文件,但不包括在
/etc/hosts.d/6-whitelist
中找到的任何行。使用的
grep
选项有:

  • -f/etc/hosts.d/6-whitelist
    告诉grep从命名文件中获取要匹配的模式列表

  • -F
    告诉grep将这些模式视为固定字符串,而不是正则表达式。除非您需要正则表达式功能,否则这会加快速度

  • -h
    告诉grep不要打印行来自的文件名

  • -v
    反转匹配,以便只打印不在白名单中的行

笔记 为了便于携带,我删除了关键字
function
。Bash不需要它,其他shell也不会接受它