删除两个字符串之间的行

时间:2018-10-23 15:23:27

标签: powershell

我有一个以下格式的文本文件:

line1  
line2  
Notes:some text here   
and also some text here  
Path : a path   
line3   
line4   
Notes:some text here   
and also some text here  
and some text here    
Path : a path  
line5   
line6   
Notes:  
some text here   
and also some text here  
and some text here    
Path : a path  

尝试删除带注释的行,并删除Path之前的所有文本。我希望输出看起来像这样:

line1  
line2  
Path : a path   
line3   
line4   
Path : a path  
line5  
line6  
Path : a path 

itried below code:  

    $File = 'D:\test.txt'
    $text = Get-Content -Path 'D:\test.txt'
    $Pattern = '(?m)^Notes:[\r\n]+.*?[\r\n]Path[\r\n]'
    $text -replace $Pattern, ' ' | 
    Out-File -FilePath $File -Force 

0 个答案:

没有答案