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

Store Advs 1 Reputation point
2021-03-07T00:23:43.3+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-03-07T03:07:04.86+00:00

    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
    
    1 person found this answer helpful.