搜索巨大的日志文件

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>

}

回答:

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

回答:

这无疑将是对你只使用基于你有没有文件大小的任何解析器难以置信的痛苦,但如果你的日志文件的格式,是标准的(例如IIS日志文件),那么你可以考虑使用日志解析应用程序,如Log Parser Studio而不是Powershell?

以上是 搜索巨大的日志文件 的全部内容, 来源链接: utcz.com/qa/266696.html

回到顶部