1

To remove the lines that contain the word pizza from the following text file:

The cat is my favorite animal.
I prefer pizza to vegetables.
My favorite color is blue.
Tennis is the only sport I like.
My favorite leisure time activity is reading books.

I ran the following code and it successfully removed the second line.

$inputFile =  "C:\Temp\Text.txt"
Get-Content $inputFile | Where-Object {$_ -notmatch 'pizza'} | Set-Content "C:\Temp\Filtered.txt"

However, I haven't found a way to remove all occurrences of lines which contain either the word pizza or the word sport. I've tried to do this with this code:

$inputFile =  "C:\Temp\Text.txt"
Get-Content $inputFile | Where-Object {$_ -notmatch 'pizza' -or $_ -notmatch 'sport'} | Set-Content "C:\Temp\Filtered.txt"

But it doesn't work as the output file is the same as the original one.

5
  • 1
    If you don't want to match either you need to use -and not -or. Otherwise you are only filtering out lines with both pizza and sport. Commented Mar 15, 2017 at 16:32
  • -and, not -or. And is inclusive, or is one or the other... Commented Mar 15, 2017 at 16:32
  • 3
    I find Where-Object {$_ -notmatch 'pizza|sport'} is a nicer way of matching multiple conditions Commented Mar 15, 2017 at 16:34
  • The question has few to do with PowerShell issues, it's about applying basic tautologies of propositional logic, particularly De Morgan's Theorems (De Morgan's laws). Commented Mar 15, 2017 at 17:15
  • @JosefZ What I need to know is how to remove all the lines in my text file that contain the word 'pizza' or that contains the work 'sport' and I don't know how to do it in PowerShell. Commented Mar 15, 2017 at 20:18

2 Answers 2

4

I find Where-Object {$_ -notmatch 'this|that'} is a nicer way of matching multiple conditions as the pipe acts as -Or for you.

$inputFile =  "C:\Temp\Text.txt"
Get-Content $inputFile | Where-Object {$_ -notmatch 'pizza|sport'} | Set-Content "C:\Temp\Filtered.txt"
Sign up to request clarification or add additional context in comments.

3 Comments

It works and it's much more elegant! Thanks for your help.
You can also use the -in operator to test for the existence of your value in an array.
@blouskrine glad I could help :) If you're happy with my answer you can Mark it as Accepted.
3

You need to make yourself clear the logic.

First, use positive condition to get all the lines in my text file that contain the word 'pizza' or the word 'sport':

Get-Content $inputFile | Where-Object {$_ -match 'pizza' -or $_ -match 'sport'}

Output should be

I prefer pizza to vegetables.
Tennis is the only sport I like.

Then, negate the condition to get desired result:

Get-Content $inputFile | Where-Object { -NOT ($_ -match 'pizza' -or $_ -match 'sport') }

De Morgan's laws allow rewriting the negated condition as

Get-Content $inputFile | Where-Object { $_ -NOTmatch 'pizza' -AND $_ -NOTmatch 'sport' }

The following script poses a truth table (naive) implementation of De Morgan's laws in PowerShell:

''
'{0,-6} {1,-6}: {2,-7}  {3,-7}  {4,-7}  {5,-7}' -f 'P', 'Q', 'DM1a', 'DM1b', 'DM2a', 'DM2b'
''
ForEach ( $P in $True, $False ) { 
    ForEach ( $Q in $True, $False ) { 
        '{0,-6} {1,-6}: {2,-7}  {3,-7}  {4,-7}  {5,-7}' -f $P, $Q, 
            ( -not ( $P -and $Q ) -eq (      ( ( -not $P ) -or  ( -not $Q ) ) ) ),
            (      ( $P -and $Q ) -eq ( -not ( ( -not $P ) -or  ( -not $Q ) ) ) ),
            ( -not ( $P -or  $Q ) -eq (      ( ( -not $P ) -and ( -not $Q ) ) ) ),
            (      ( $P -or  $Q ) -eq ( -not ( ( -not $P ) -and ( -not $Q ) ) ) )
    }

}

Output (note that DM2a column covers your case):

PS D:\PShell> D:\PShell\tests\DeMorgan.ps1

P      Q     : DM1a     DM1b     DM2a     DM2b   

True   True  : True     True     True     True   
True   False : True     True     True     True   
False  True  : True     True     True     True   
False  False : True     True     True     True   

1 Comment

Oh! I get it. Thanks you for taking the time to write this long explanation. Very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.