The simple but inefficient way is to process the file multiple times, once for each input word:
$ while read w; do sed -i "s/$w//ig" file2 ; done < file1
$ cat file2
Both the and are monarchs. Will the live? , it is!
That can be very slow for large files though (and also matches substrings). You could do it in a single pass with Perl:
perl -lpe 'BEGIN{open(A,"file1"); chomp(@k = <A>)}
for $w (@k){s/\b\Q$w\E\b//ig}' file2
The \b make sure we only match on word boundaries, \Q\E make sure $w is taken literally. This will stop the script from matching hiking but it will still match high-king. To avoid that, you need to explicitly list the characters that define a word:
perl -Mopen=locale -Mutf8 -lpe '
BEGIN{open(A,"file1"); chomp(@k = <A>)}
for $w (@k){s/(^|[ ,.—_;-])\Q$w\E([ ,.—_;-]|$)/$1$2/ig}' file2
That — non-ASCII character above needs to be entered in UTF-8 encoding, as we're telling perl the code is written in UTF-8 with -Mutf8. We're using -Mopen=locale for the content of the files and stdout to be decoded/encoded in the locale's character set.
queen,matchesqueen)? How about substrings? Shouldkingmatchhiking? Orhigh-king? – terdon Nov 10 '16 at 12:26sedallows to submit expressions from a file via its option-f. You could even writesed"programs" by prepending#!/usr/bin/sed -fto such a file and giving it execute permissions. However, with 50000 expressions I agree thatsedseems to be an inappropriate choice. – countermode Nov 10 '16 at 14:02