Skip to content

Instantly share code, notes, and snippets.

@deleep000
Created January 2, 2014 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deleep000/8227629 to your computer and use it in GitHub Desktop.
Save deleep000/8227629 to your computer and use it in GitHub Desktop.
Powershell command to delete all the lines matching a word from a file.
# PowerShell
Get-Content <sometext_or_log_file> | Where-Object {$_ -notmatch "<exact string>"} | Set-Content <outputfile>
# Example
Get-Content W3000454.log | Where-Object {$_ -notmatch "ping"} | Set-Content c:\log\W3000454_no_ping.log
# The above command will strip all the lines matching the word "ping" and writes to W3000454_no_ping.log
@pradeep14333
Copy link

how to write code in Powershell If I want to remove any lines in a text file which contains any of these strings "ping" "pong" "foo"?

@uisge67
Copy link

uisge67 commented Aug 23, 2020

Looking for the same. @pradeep14333 did you every find a solution to remove lines that match a list of strings?

@spch91
Copy link

spch91 commented Aug 30, 2020

array of values can be set for regex. sample for selecting the specific lines that match that array. hope you can appropriately use the below
$regexA = "STRING1"
$regexB = "STRING2"
$filterArray = @($regexA, $regexB)
get-content input-file.txt | select-string -pattern $filterArray | Set-Content output-file.txt

eg input-file.txt:
line1 string1
line2 string2
line3 string3
line4 string4
line5 string1
line6 string2
line7 string3
line8 string4

Output-file.txt:
line1 string1
line2 string2
line5 string1
line6 string2

Copy link

ghost commented Nov 6, 2021

thanks dude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment