$logPath = "C:\logs\access.log"
$outputPath = "C:\logs\top_urls_by_second.csv"
读取日志文件,并提取时间戳和URL
$logs = Get-Content $logPath | ForEach-Object {
if ($_ -match "[(\d+)/\w+/\d+:(\d+):(\d+):(\d+)] "\w+ (\S+)") {
$time = "{0:D2}:{1:D2}:{2:D2}" -f $matches[2], $matches[3], $matches[4]
$url = $matches[5]
[PSCustomObject]@{
Time = $time
URL = $url
}
}
}
分组统计每秒的URL访问次数
$groupedLogs = $logs | Group-Object Time, URL | ForEach-Object {
[PSCustomObject]@{
Time = $.Name.Split(',')[0].Trim()
URL = $.Name.Split(',')[1].Trim()
Count = $_.Group.Count
}
} | Sort-Object Time, Count -Descending
选择每秒访问次数前3的URL
$result = @()
$groupedLogs | Group-Object Time | ForEach-Object {
$timeGroup = $_
$topUrls = $_.Group | Sort-Object Count -Descending | Select-Object -First 3
foreach ($url in $topUrls) {
$result += [PSCustomObject]@{
Time = $timeGroup.Name
URL = $url.URL
Count = $url.Count
}
}
}
输出结果到CSV文件
$result | Export-Csv -Path $outputPath -NoTypeInformation