使用PowerShell从包含字符串的文本文件中删除行

使用PowerShell从包含字符串的文本文件中删除行,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我正在尝试使用以下PowerShell代码删除包含部分字符串的文本文件中的所有行: Get-Content C:\new\temp_*.txt | Select-String -pattern "H|159" -notmatch | Out-File C:\new\newfile.txt 实际字符串是H | 159 | 28-05-2005 | 508 | xxx,它在文件中重复多次,我尝试只匹配上面指定的第一部分。对吗?目前我的输出为空 我遗漏了什么吗?管道字符|在正则表达式中有特殊的含义

我正在尝试使用以下PowerShell代码删除包含部分字符串的文本文件中的所有行:

 Get-Content C:\new\temp_*.txt | Select-String -pattern "H|159" -notmatch | Out-File C:\new\newfile.txt
实际字符串是
H | 159 | 28-05-2005 | 508 | xxx
,它在文件中重复多次,我尝试只匹配上面指定的第一部分。对吗?目前我的输出为空


我遗漏了什么吗?

管道字符
|
在正则表达式中有特殊的含义
a | b
表示“匹配
a
b
”。如果要匹配文字
|
字符,需要对其进行转义:

... | Select-String -Pattern 'H\|159' -NotMatch | ...

使用反勾号转义|字符

get-content c:\new\temp_*.txt | select-string -pattern 'H`|159' -notmatch | Out-File c:\new\newfile.txt

假设要将其写入同一文件中,可以执行以下操作:

Set-Content -Path "C:\temp\Newtext.txt" -Value (get-content -Path "c:\Temp\Newtext.txt" | Select-String -Pattern 'H\|159' -NotMatch)

另一个选项是在现有答案的基础上写入同一文件。在将内容发送到文件之前,只需添加括号即可完成操作

(get-content c:\new\sameFile.txt | select-string -pattern 'H`|159' -notmatch) | Out-File c:\new\sameFile.txt

在这种情况下,您不需要
选择String
,只需使用


执行字符串比较而不是正则表达式,这样就不需要转义管道字符,而且速度更快。这可能是一个简单问题,它允许我删除包含大量匹配项的行。我没有可以使用的部分匹配,需要对1000多个文件进行匹配。 这篇文章确实帮助我到达了我需要的地方,谢谢

$ParentPath = "C:\temp\test"
$Files = Get-ChildItem -Path $ParentPath -Recurse -Include *.txt
$Match2 = "matchtext1"
$Match2 = "matchtext2"
$Match3 = "matchtext3"
$Match4 = "matchtext4"
$Match5 = "matchtext5"
$Match6 = "matchtext6"
$Match7 = "matchtext7"
$Match8 = "matchtext8"
$Match9 = "matchtext9"
$Match10 = "matchtext10"

foreach ($File in $Files) {
    $FullPath = $File | % { $_.FullName }
    $OldContent = Get-Content $FullPath
    $NewContent = $OldContent `
    | Where-Object {$_ -notmatch $Match1} `
    | Where-Object {$_ -notmatch $Match2} `
    | Where-Object {$_ -notmatch $Match3} `
    | Where-Object {$_ -notmatch $Match4} `
    | Where-Object {$_ -notmatch $Match5} `
    | Where-Object {$_ -notmatch $Match6} `
    | Where-Object {$_ -notmatch $Match7} `
    | Where-Object {$_ -notmatch $Match8} `
    | Where-Object {$_ -notmatch $Match9} `
    | Where-Object {$_ -notmatch $Match10}
    Set-Content -Path $FullPath -Value $NewContent
    Write-Output $File
}

在PowerShell中,转义字符是backtick(`)。看,奥拉德,我知道这一点。但是,在正则表达式中,转义字符是反斜杠。在这种情况下两者都有效。这正是我想要的。Thx@Samselvaprabu!警告-我使用此选项试图就地更新文件,文件已被删除。
$ParentPath = "C:\temp\test"
$Files = Get-ChildItem -Path $ParentPath -Recurse -Include *.txt
$Match2 = "matchtext1"
$Match2 = "matchtext2"
$Match3 = "matchtext3"
$Match4 = "matchtext4"
$Match5 = "matchtext5"
$Match6 = "matchtext6"
$Match7 = "matchtext7"
$Match8 = "matchtext8"
$Match9 = "matchtext9"
$Match10 = "matchtext10"

foreach ($File in $Files) {
    $FullPath = $File | % { $_.FullName }
    $OldContent = Get-Content $FullPath
    $NewContent = $OldContent `
    | Where-Object {$_ -notmatch $Match1} `
    | Where-Object {$_ -notmatch $Match2} `
    | Where-Object {$_ -notmatch $Match3} `
    | Where-Object {$_ -notmatch $Match4} `
    | Where-Object {$_ -notmatch $Match5} `
    | Where-Object {$_ -notmatch $Match6} `
    | Where-Object {$_ -notmatch $Match7} `
    | Where-Object {$_ -notmatch $Match8} `
    | Where-Object {$_ -notmatch $Match9} `
    | Where-Object {$_ -notmatch $Match10}
    Set-Content -Path $FullPath -Value $NewContent
    Write-Output $File
}