Created
January 2, 2014 21:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Looking for the same. @pradeep14333 did you every find a solution to remove lines that match a list of strings?
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
thanks dude
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"?