EngConI需要从Select-String命令中获取信息的帮助。我需要找出engcon.pbo文件中有哪些来自$array的项。如果找到的结果显示在控制台上,或者甚至更好,在.txt文件中显示,这将是最有用的。
在完整代码中,数组中有297项(包括#0和#295 )。
######
#Mine#
######
$TargetFile = "C:\PowershellScripts\EngCon.pbo"
$array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate")
$i = 0
while ($i -le 295)
{
$SearchString = $array[$i]
Select-String $TargetFile -pattern $SearchString
$i = $i + 1
}
#######
#Yours#
#######
$array = @($array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate")
$found = @{}
Get-Content "C:\PowershellScripts\EngCon.txt" | % {
$line = $_
foreach ($item in $array) {
if ($line -match $item) { $found[$item] = $true }
}
}
$found.Keys | Out-File "C:\PowershellScripts\results.txt"如果可能的话,你能提供一些学习PS的好地方吗?
在使用"write-host“快速测试之后,结果显示foreach ($item in $array)中的某些东西导致了错误(脚本立即结束),而且我使用的示例文件只是一些数组项和一些随机单词的测试器,所有这些都由空格分隔。至于代码,我所编辑的都是$array中的项目集
仅供参考,我不能透露大多数数组项,因为它们是私有的
“Uncovenable Marsileaceae extreme random Tribunitious”是适用于所有版本(EngCon.pbo、EngCon.txt和EngCon)的完整测试仪文件。
发布于 2013-06-30 19:39:42
您的Select-String指令正在重复读取$TargetFile。这将对性能产生不利影响。试试下面这样的代码:
$array = @(...)
$found = @{}
Get-Content "C:\PowershellScripts\EngCon.pbo" | % {
$line = $_
foreach ($item in $array) {
if ($line -match $item) { $found[$item] = $true }
}
}
$found.Keys | Out-File "C:\results.txt"https://stackoverflow.com/questions/17389784
复制相似问题