question

StoreAdvs-5792 avatar image
0 Votes"
StoreAdvs-5792 asked ·

Powershell - Delete all lines that contain a string from a seperated file

Hi,

i need to remove swear words from a .txt file based on strings on a keyword.txt file.
Example:

Input.txt contains:
We go too the zoo.
Whats the weather today noob?
Let me be stupid.
Why is it raining?

Keyword.txt contains:
stupid
noob

I want to filter out all lines that match if a keyword (line by line) in the keyboard.txt

So result.txt would be:
We go too the zoo.
Why is it raining?

Could anyone help me out?

windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered ·

See how this works for you:

 $k = Get-Content C:\Junk\BadWordList.txt
 Get-Content C:\Junk\LinesToScrub.txt | 
     ForEach-Object{
         $badword = $false
         ForEach($w in $k){
             if ($_ -match "\b$w\b"){  # match whole word only...i.e., don't find "twat" in "saltwater"
                 $badword = $true
                 break
             }
         }
         if (-not $badword){
             $_
         }
     } | Out-File C:\Junk\NoBadWords.txt
· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

thank you.

But this doesn't seem to be working for me. I tried to adjust a few things that might cause the issue but yeah.

0 Votes 0 ·

Thanks for that in-depth description of your problem. Good luck with your homework assignment!

Here are the files I tested with:
75076-lines.txt75123-wordlist.txt


And here's the output:
75064-nobadwords.txt


1 Vote 1 ·
lines.txt (87 B)
wordlist.txt (12 B)