Remove duplicates from text file based on second text file

There are two standard ways to do this:

With grep:

grep -vxFf removethese main

This uses:

  • -v to invert the match.
  • -x match whole line, to prevent, for example, he to match lines like hello or highway to hell.
  • -F to use fixed strings, so that the parameter is taken as it is, not interpreted as a regular expression.
  • -f to get the patterns from another file. In this case, from removethese.

With awk:

$ awk 'FNR==NR {a[$0];next} !($0 in a)' removethese main
1
5

Like this we store every line in removethese in an array a[]. Then, we read the main file and just print those lines that are not present in the array.

Leave a Comment