exclude search pattern with select-string in powershell

exclude search pattern with select-string in powershell

I use -NotMatch parameter for this

PS C:>Get-Content .some.txt
1
2
3
4
5
PS C:>Get-Content .some.txt | Select-String -Pattern 3 -NotMatch    
1
2
4
5

For your case the answer is:

Get-Content .logerror.txt | Select-String -Pattern ERR* | Select-String -Pattern ERR-[01]0 -NotMatch

I guess you can use Where-Object here.

Write-Output @
OK
ERR-10
OK
OK
ERR-20
OK
OK
ERR-10
ERR-00
@ > C:templog.txt

# Option 1.
Get-Content C:templog.txt | Where-Object { $_ -Match ERR*} | Where-Object { $_ -NotMatch ERR-[01]0}

# Option 2.
Get-Content C:templog.txt | Where-Object { $_ -Match ERR* -and $_ -NotMatch ERR-[01]0}

exclude search pattern with select-string in powershell

Leave a Reply

Your email address will not be published. Required fields are marked *