我有一个正则表达式列表(大约2000个)和超过一百万个html文件。我想检查每个文件上的每个正则表达式是否成功。如何在Powershell上执行此操作?
性能很重要,因此我不想遍历正则表达式。
我尝试
$text | Select-String -Pattern pattern1, pattern2,...
它返回所有匹配项,但我也想找出哪种模式成功,哪些没有。我需要为每个文件构建成功正则表达式列表
您可以尝试这样的事情:
$regex = "^test","e2$" #Or use (Get-Content <path to your regex file>)
$ht = @{}
#Modify Get-Childitem to your criterias(filter, path, recurse etc.)
Get-ChildItem -Filter *.txt | Select-String -Pattern $regex | ForEach-Object {
$ht[$_.Path] += @($_ | Select-Object -ExpandProperty Pattern)
}
测试输出:
$ht | Format-Table -AutoSize
Name Value
---- -----
C:\Users\graimer\Desktop\New Text Document (2).txt {e2$}
C:\Users\graimer\Desktop\New Text Document.txt {^test, e2$}
您没有指定所需的输出方式。
更新:要在一行上匹配多个模式,请尝试使用此方法(mjolinor的答案可能比此方法更快)。
$regex = "^test","e2$" #Or use (Get-Content <path to your regex file>)
$ht = @{}
#Modify Get-Childitem to your criterias(filter, path, recurse etc.)
$regex | ForEach-Object {
$pattern = $_
Get-ChildItem -Filter *.txt | Select-String -Pattern $pattern | ForEach-Object {
$ht[$_.Path] += @($_ | Select-Object -ExpandProperty Pattern)
}
}
UPDATE2:我没有足够的示例来尝试它,但是由于您有如此大量的文件,因此您可能希望尝试在遍历模式之前将文件读入内存。可能会更快。
$regex = "^test","e2$" #Or use (Get-Content <path to your regex file>)
$ht = @{}
#Modify Get-Childitem to your criterias(filter, path, recurse etc.)
Get-ChildItem -Filter *.txt | ForEach-Object {
$text = $_ | Get-Content
$filename = $_.FullName
$regex | ForEach-Object {
$text | Select-String -Pattern $_ | ForEach-Object {
$ht[$filename] += @($_ | Select-Object -ExpandProperty Pattern)
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句