I have a long list of domain names that I need to remove from /etc/remotedomains. They're probably not in any particular order in the file. Each domain is on one line.

How could I iterate through the list and find that line in remote domains and remove it.

3 Answers 3

grep -Fxf list -v /etc/remotedomains > remotedomains.new
mv remotedomains.new /etc/remotedomains

The -v tells grep to only output lines that don't match the pattern.

The -f list tells grep to read the patterns from the file list.

The -F tells grep to interpret the patterns as plain strings, not regular expressions (so you won't run into trouble with regex meta-characters).

The -x tells grep to match the whole line, e.g. if there's a pattern foo that should only remove the line foo, not the line foobar or barfoo.

  • 3
    note: always remember to backup /etc/localdomains and /etc/remotedomains before doing stuff like this. Oct 24, 2010 at 4:03
  • 3
    -F is for fixed string matching (“exact matches”), but it does not force the pattern to match the whole line. POSIX specifies the -x flag to limit matches to instances where a pattern matches the whole line. Nov 6, 2010 at 3:14
  • @Chris: Bah, good catch.
    – sepp2k
    Nov 7, 2010 at 0:22

Use comm!

comm -23 /etc/remotedomains remove

From the man page:

Compare sorted files FILE1 and FILE2 line by line.

With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.

Options -1, -2 and -3 disable respective columns.

It does however require that files be sorted.


sed -r -i -e '/^(domain1|domain2|domain3|...|domainN)$/d' domainlist.txt

If your list of domains to delete is in a file then:

sed -r -i -e '/^'"$(xargs <removelist.txt|sed -e 's/ /|/g)"')$/d' domainlist.txt


  1. Use -i.bak to have sed make a backup of domainlist.txt

  2. If removelist.txt has many thousands of entries, it could overflow the maximum command line length. This is not a very likely scenario but something you should be aware of.

On further reflection, that risk can be eliminated with:

for RE in $(xargs <removelist.txt|sed -e 's/ /|/g') ; \
  do sed -r -i -e '/^('"$RE"')$/d' domainlist.txt ; \

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.