Deleting entire lines in a text file based on a partial string match with Windows PowerShell
Posted
by Charles
on Super User
See other posts from Super User
or by Charles
Published on 2010-03-08T21:24:26Z
Indexed on
2010/03/08
21:38 UTC
Read the original article
Hit count: 610
So I have several large text files I need to sort through, and remove all occurrences of lines which contain a given keyword. So basically, if I have these lines:
This is not a test This is a test Maybe a test Definitely not a test
And I run the script with 'not', I need to entirely delete lines 1 and 4.
I've been trying with:
PS C:\Users\Admin> (Get-Content "D:\Logs\co2.txt") | Foreach-Object {$_ -replace "3*Program*", ""} | Set-Content "D:\Logs\co2.txt"
but it only replaces the 'Program' and not the entire line.
© Super User or respective owner