Let's say I have this file (file.txt
):
Hello my name is Giorgio,
I would like to go with you
to the cinema my friend
I want to exclude from the text the words: my
, is
and I
(not the whole line).
The words are in a file (words.txt
) like this:
my
is
I
So the output must be:
Hello name Giorgio,
would like to go with you
to the cinema friend
How can this be performed?
You can use sed to turn words.txt into a sed script:
The difference to the expected output is the whitespace: removing a word doesn't squeeze the surrounding whitespace.
To match only whole words, add the word boundaries
\b
:Perl solution that also squeezes the spaces (and doesn't care about meta characters):