Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 9 down vote accepted
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.

share|improve this answer
note: always remember to backup /etc/localdomains and /etc/remotedomains before doing stuff like this. – xenoterracide Oct 24 '10 at 4:03
-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. – Chris Johnsen Nov 6 '10 at 3:14
@Chris: Bah, good catch. – sepp2k Nov 7 '10 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.

share|improve this answer

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 ; \
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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