Using PowerShell to remove lines from a text file if it contains a string

前端 未结 3 1433
轮回少年
轮回少年 2020-12-06 16:12

I am trying to remove all the lines from a text file that contains a partial string using the below PowerShell code:

 Get-Content C:\\new\\temp_*.txt | Selec         


        
相关标签:
3条回答
  • 2020-12-06 16:49

    Suppose you want to write that in the same file, you can do as follows:

    Set-Content -Path "C:\temp\Newtext.txt" -Value (get-content -Path "c:\Temp\Newtext.txt" | Select-String -Pattern 'H\|159' -NotMatch)
    
    0 讨论(0)
  • 2020-12-06 16:49

    The pipe character | has a special meaning in regular expressions. a|b means "match either a or b". If you want to match a literal | character, you need to escape it:

    ... | Select-String -Pattern 'H\|159' -NotMatch | ...
    
    0 讨论(0)
  • 2020-12-06 17:03

    Escape the | character using a backtick

    get-content c:\new\temp_*.txt | select-string -pattern 'H`|159' -notmatch | Out-File c:\new\newfile.txt
    
    0 讨论(0)
提交回复
热议问题