2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShell 集計

Posted at

$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

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?