Bash Remove Lines From a File Matching Another File

Some times little nuggets can make your day. Yes its the simple things that can make you smile special when its a 1 liner that could of been a bit of pain.

Grep is one of the tools I would use more than any other and today found a neat trick that allows you to take a file and filter from another file.

Was cleaning up a old name server which had been neglected for too long and a lot of old domains it no longer needed to serve for.

file full of domains to remove – domains-to-remove.txt

domain.com
anotherdomain.com

named file with single line per domain – named.conf.old

domain.com
anotherdomain.com
somereallycooldomain.com
mustkeepthisone.com

Now for some dead simple grep

grep -v -F -f domains-to-remove.txt named.conf.old > named.conf.old.new

Now you will have a file named named.conf.old.new with the domains from domains-to-remove.txt removed.

Leave a Reply