Deleting a list of words from a text file

Hello,

I have a list of words separated by spaces I am trying to delete from a text file, and I could not figure out what is the best way to do this.

what I tried (does not work) :

delete="password key number verify"
arr=($delete)
for i in arr
{
   sed "s/\<${arr}\>[[:space:]]*//g" in.txt 
}
> out.txt

I have hundreds of words I want to delete, and I don't want to put all of them inside the quotes of sed nor inside an external file.
Please any suggestions how to solve the issue? any other efficient ways?

awk ' {gsub("password|key|number|verify","") } 1 '  in.txt > out.txt

Welcome to the forum.

How do you plan to deliver the "hundreds of words I want to delete" if you "don't want to put all of them inside the quotes of sed nor inside an external file"?

1 Like

anbu23 looks like it is working. the problem is that it is also deleting sub strings and not just the word. in addition, is there any option to delete spaces that come after the deleted word?

say I have a sentence: The password and the key are safe in the vault - please verify.
and after the script I should get : The and are safe in the vault - please.

Thank you RudiC, and you are right - that makes no sense. what i meant is that i don't want to manually separate the words with whatever separator is needed - but guess I can just write a script for that.

awk ' { gsub("\\<(password|key|number|verify)\\> *","") } 1 '  in.txt > out.txt

Thank you !