Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

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

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

Notes:

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

Your Answer

 
discard

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.