2017-06-16 19 views
0

這裏是我使用的腳本,傳入的文件大約500MB是什麼讓這個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」cmdlet非常有用,但不是很快(IMO)。 – TheMadTechnician

+0

同意@TheMadTechnician有關Group-Object緩慢。如果目標只是獲取匹配值的集合,則Select-String也比-match運算符慢得多。 – mjolinor

回答

1

我想交易select-stringGet-ContentReadCount爲1000-5000,然後使用-match作爲數組運算符對結果行數組。將字符串匹配到哈希表累加器以獲取計數。

未經測試。

$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