How do I take a list and remove it from a file?

6,879

Solution 1

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.

Solution 2

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:
6,879

Related videos on Youtube

xenoterracide
Author by

xenoterracide

Former Linux System Administrator, now full time Java Software Engineer.

Updated on September 17, 2022

Comments

  • xenoterracide
    xenoterracide over 1 year

    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.

  • xenoterracide
    xenoterracide over 13 years
    note: always remember to backup /etc/localdomains and /etc/remotedomains before doing stuff like this.
  • Chris Johnsen
    Chris Johnsen over 13 years
    -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.
  • sepp2k
    sepp2k over 13 years
    @Chris: Bah, good catch.