How to delete all occurrences of a list of words from a text file

grepsedtext processingtext;

I have a file containing a list of words. I want to remove all occurrences of all the words in this file from a big text file.

Example:

File 1

queen
king

Text file sample

Both the king and queen are monarchs. Will the queen live? Queen, it is!

This is what I have tried:

sed -i 's/queen/ /g' page.txt
sed -i 's/Queen/ /g' page.txt

Output

Both the and are monarchs. Will the live? , it is!

The list of words I have is big (over 50000 words). How can I do this without having to specify the pattern in the command line?

Best Answer

For your actual use case I recommend terdon's answer using Perl.

However, the simple version, without handling words that are substrings of other words (e.g. removing "king" from "hiking"), is to use one Sed command to generate the command run by a different Sed instance on your actual file.

In this case, with wordfile containing "king" and "queen" and textfile containing your text:

sed -e "$(sed 's:.*:s/&//ig:' wordfile)" textfile

Note that the "ignore case" flag is a GNU extension, not standard.

Related Question