There's more important issues with your script than execution speed, it'll also encounter false matches in 2 ways:
- Regexp vs String: You're using a regexp comparison when you should be using a string comparison. As written the
.
s from the IP addresses in RemoveTheseGoodIPs.txt will match any character FromTheseShadyIPs.txt, and
- Partial vs Full: You're using a partial line comparison when you should be using a full-line comparison. As written a shorter IP address in RemoveTheseGoodIPs.txt will match any IP address that includes it in FromTheseShadyIPs.txt
Given that, your current script is almost certainly removing IP addresses from FromTheseShadyIPs.txt that are not present in RemoveTheseGoodIPs.txt, thereby effectively breaking your firewall.
For example, if RemoveTheseGoodIPs.txt contained 1.2.3.4
and FromTheseShadyIPs.txt contained 911.253.456.789
then your grep would remove that 2nd IP address because you're doing a partial-line regexp match instead of the full-line string match which you need:
$ head RemoveTheseGoodIPs.txt FromTheseShadyIPs.txt
==> RemoveTheseGoodIPs.txt <==
1.2.3.4
==> FromTheseShadyIPs.txt <==
9.8.7.6
911.253.456.789
6.7.8.9
$ grep -v -f RemoveTheseGoodIPs.txt FromTheseShadyIPs.txt
9.8.7.6
6.7.8.9
You should be using
$ grep -vFxf RemoveTheseGoodIPs.txt FromTheseShadyIPs.txt
9.8.7.6
911.253.456.789
6.7.8.9
to make your script work. That's -F
for string instead of regexp comparison, and -x
for full-line instead of partial comparison. That will probably also be faster than your current script but the far more important difference is it'll work robustly.
If your grep doesn't support any of those options and you can't get a version that does then use the following with any awk instead:
$ awk 'NR==FNR{a[$0]; next} !($0 in a)' RemoveTheseGoodIPs.txt FromTheseShadyIPs.txt
9.8.7.6
911.253.456.789
6.7.8.9
As @Paul_Pedant mentions in a comment using awk may be faster than grep for this anyway, depending on your grep and awk implementations.
diff
as described here: unix.stackexchange.com/questions/56625/… But I agree with Anthony that your problem unlikely this grep command. I tried it with 200k random IPs and 3k to filter. Took only 3.3s with your commands and not even 100ms with-F
flag.grep -F
can run faster thangrep
if you're able to provide fixed strings instead of regular expressions.