2017-06-16 5 views
0

は、私が使用しているスクリプトで、渡されたファイルは500メガバイトこのPowerShellスクリプトがファイルの行を読みにくくなる原因は何ですか?ここで

$file=$args[0] 

If ($args[1] -eq 'response') { 
$results = Select-String -Path $file -Pattern "(?<=sent:).+(?= type)" | Select -Expand Matches | Select -Expand Value 
} 

If ($args[1] -eq 'blocked') { 
$results = Select-String -Path $file -Pattern "(?<=:).+(?= ->)" | Select -Expand Matches | Select -Expand Value 
} 

If ($args[1] -eq 'clients') { 
$results = Select-String -Path $file -Pattern "(?<=:\d\d).+(?= \[)" | Select -Expand Matches | Select -Expand Value 
} 

$results | Group-Object | Select-Object Name,Count | Sort-Object Count -Descending 

についてですこの同じデータを取得するためのより高速な方法はありますか?私はPowerShellと結婚していません。

+1

スクリプトが何をするべきかを説明すれば、より良い応答を得られるはずです(人々は正規表現を解析して把握する必要はありません)。 –

+1

'-ReadCount 1000'のように' Get-Content'を使い、それを 'Select-String'にパイプしますか?ボトルネックが最終行ではないと確信していますか? 'Group-Object'コマンドレットは非常に便利ですが、高速ではありません(IMO)。 – TheMadTechnician

+0

@TheMadTechnicianとのグループオブジェクトが遅いと同意します。 Select-Stringは、マッチした値のコレクションを取得するだけの目的であれば、-match演算子よりもはるかに低速です。 – mjolinor

答えて

1

Iその後、得られたラインアレイに対する配列演算子として-matchを使用し、1000〜5000のReadCountと、Get-Contentためselect-stringを交換したいです。文字列の一致をハッシュテーブルアキュムレータに送り、カウントを取得します。

テストされていません。

$file=$args[0] 
$ht = @{} 

If ($args[1] -eq 'response') { 
$results = Get-Content $file -ReadCount 1000 | 
    foreach-object { 
    $_ -match "(?<=sent:).+(?= type)" | 
    ForEach-Object { $ht[$_]++ } 
    } 
} 

If ($args[1] -eq 'blocked') { 
$results = Get-Content $file -ReadCount 1000 | 
    foreach-object { 
    $_ -match "(?<=:).+(?= ->)"| 
    ForEach-Object { $ht[$_]++ } 
    } 
} 

If ($args[1] -eq 'clients') { 
$results = Get-Content $file -ReadCount 1000 | 
    foreach-object { 
    $_ -match "(?<=:\d\d).+(?= \[)"| 
    ForEach-Object { $ht[$_]++ } 
    } 
} 

$results.GetEnumerator() | Sort-Object Value -Descending 
関連する問題