2013-02-26 72 views
0

Powershell的問題搜索巨大的日誌文件

多個字符串目前我有5-10日誌文件,所有關於20-25GB每需要通過他們每個人的搜索,以檢查是否有任何的900多種不同的搜索參數匹配。我寫了一個基本的PowerShell腳本,將搜索整個日誌文件中的1個搜索參數。如果匹配將傾倒出來的結果到一個單獨的文本文件,該問題是,它是相當緩慢。我想知道如果有一種方法通過既可以加快這使得它在一次只有通過日誌看一次搜索所有的900個參數。即使只是改善腳本,任何幫助都會很好。

基本概述:與 「項目」 列下列所有的900項

1 csv文件 1個日誌文件(.txt) 1結果文件(.txt) 1 PS1文件

這裏是我的代碼下面有PowerShell的一個PS1文件:

$search = filepath to csv file<br> 
$log = "filepath to log file"<br> 
$result = "file path to result text file"<br> 
$list = import-csv $search <br> 


foreach ($address in $list) {<br> 
Get-Content $log | Select-String $address.item | add-content $result <br> 

*"#"below is just for displaying a rudimentary counter of how far through searching it is <br>* 
$i = $i + 1 <br> 
echo $i <br> 
} 

回答

0

900搜索而言是相當大的一個羣體。你能用正則表達式來縮小它的大小嗎?一個簡單的解決方案是基於逐行閱讀文件並尋找匹配。設置包含搜索項的正則表達式或文字字符串的集合。像這樣,

$terms = @("Keyword[12]", "KeywordA", "KeyphraseOne") # Array of regexps 
$src = "path-to-some-huge-file" # Path to the file 
$reader = new-object IO.StreamReader($src) # Stream reader to file 

while(($line = $reader.ReadLine()) -ne $null){ # Read one row at a time 

    foreach($t in $terms) { # For each search term... 
     if($line -match $t) { # check if the line read is a match... 
      $("Hit: {0} ({1})" -f $line, $t) # and print match 
     } 
    } 
} 
$reader.Close() # Close the reader 
+0

我同意。如果可能的話,縮小搜索條件的數量將會有很大的幫助。 StreamReader的使用將減少它自己的時間。你有沒有考慮使用'Select-String -Pattern $ terms'來匹配呢?它將一個數組作爲模式輸入。不確定如何比較foreach的性能。 – 2013-02-26 07:56:03

0

這無疑將是對你只使用基於你有沒有文件大小的任何解析器難以置信的痛苦,但如果你的日誌文件的格式,是標準的(例如IIS日誌文件),那麼你可以考慮使用日誌解析應用程序,如Log Parser Studio而不是Powershell?