【问题标题】:Need to make search in PowerShell script faster需要更快地在 PowerShell 脚本中进行搜索
【发布时间】:2017-04-11 02:51:39
【问题描述】:

谁能帮助加快搜索速度?使用此代码,搜索需要几天时间。

Search_Names.csv(大约 10k 个名字)

Need_This_Long_Strings.csv(大约 180k 个字符串,大小为 50MB)

$TimeStamp = Get-Date -Format {yyyy.MM.dd_hh.mm.ss}
$SearchNames = gc D:\Search_Names.csv
$WhereSearch = gc D:\Need_This_Long_Strings.csv
$Val = 0
 
foreach ($SearchName in $SearchNames)
{
       $WhereSearch | Where{$_ | Select-String -Pattern "$SearchName.*"} | Out-File D:\Find_in_Search_File_$TimeStamp.log -Append
       $Val = $Val + 1
}
"Count of matches - $Val" |Out-File D:\Find_in_Search_File_$TimeStamp.log -Append

【问题讨论】:

    标签: performance search select-string fastercsv boost-foreach


    【解决方案1】:

    我自己找到了解决方案。 刚刚将数据(Need_This_Long_Strings.csv)制作成一个数组。 现在这个代码搜索大约需要 20 分钟。

    $TimeStamp = Get-Date -Format {yyyy.MM.dd_hh.mm.ss}
    $SearchNames = Get-Content D:\Search_Names.csv
    $WhereSearch = Import-Csv D:\Need_This_Long_Strings.csv -Delimiter ";"
    $SearchArray = New-Object System.Collections.ArrayList($null)
    $SearchArray.AddRange($WhereSearch)
    $Val = 0
    
    foreach ($_ in $SearchArray)
    {
           if ($SearchNames -contains $_.FullName)
           {
                 $_ | Export-Csv D:\Find_in_Search_File_$TimeStamp.csv -Delimiter ";" -Append
                 $Val = $Val + 1
           }
    }
    "Count of matches - $Val" |Out-File D:\Find_in_Search_File_$TimeStamp.log -Append
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2018-08-08
      相关资源
      最近更新 更多