【发布时间】:2015-09-15 22:07:16
【问题描述】:
我从日志中收集了一组字符串,我试图将其解析为唯一条目:
function Scan ($path, $logPaths, $pattern)
{
$logPaths | % `
{
$file = $_.FullName
Write-Host "`n[$file]"
Get-Content $file | Select-String -Pattern $pattern -CaseSensitive - AllMatches | % `
{
$regexDateTime = New-Object System.Text.RegularExpressions.Regex "((?:\d{4})-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}(,\d{3})?)"
$matchDate = $regexDateTime.match($_)
if($matchDate.success)
{
$loglinedate = [System.DateTime]::ParseExact($matchDate, "yyyy-MM-dd HH:mm:ss,FFF", [System.Globalization.CultureInfo]::InvariantCulture)
if ($loglinedate -gt $laterThan)
{
$date = $($_.toString().TrimStart() -split ']')[0]
$message = $($_.toString().TrimStart() -split ']')[1]
$messageArr += ,$date,$message
}
}
}
$messageArr | sort $message -Unique | foreach { Write-Host -f Green $date$message}
}
}
所以对于这个输入:
2015-09-04 07:50:06 [20] WARN Core.Ports.Services.ReferenceDataCheckers.SharedCheckers.DocumentLibraryMustExistService - 找不到 DocumentLibrary 3。
2015-09-04 07:50:06 [20] WARN Core.Ports.Services.ReferenceDataCheckers.SharedCheckers.DocumentLibraryMustExistService - 找不到 DocumentLibrary 3。
2015-09-04 07:50:16 [20] WARN Brighter - 消息 abc123 已被消费者标记为已过时,因为实体在消费者端具有更高版本。
只应返回后两个条目
我在过滤掉重复的 $message 时遇到了麻烦:目前所有条目都被返回(sort -Unique 的行为不像我预期的那样)。我还需要针对过滤后的 $message 返回正确的 $date。
我很困惑,有人可以帮忙吗?
【问题讨论】:
-
我无法理解您使用的文本格式,因为我看不到输入。作为一般建议,请尝试通过管道连接到 Group-Object。它会自动对唯一值进行分组,并为您提供每个值的总数。
标签: arrays sorting powershell split unique