# 検索するディレクトリのパス
$prefix_directory = "D:\ドキュメント\個人work\01‗work\02_IT関連\20241118_リクエスト数カウントスクリプト"
# 検索するディレクトリのパス
$directory = "${prefix_directory}\target"
# キーワードファイルと除外キーワードファイルのパス
$searchKeywordsFile = "${prefix_directory}\keyword\search_keywords.txt"
$excludeKeywordsFile = "${prefix_directory}\keyword\exclude_keywords.txt"
# 外部ファイルからキーワードを読み込み
$searchKeywords = Get-Content -Path $searchKeywordsFile
$excludeKeywords = Get-Content -Path $excludeKeywordsFile
# キーワードごとのヒット件数を記録するためのハッシュテーブルを初期化
$keywordCounts = @{}
# キーワードをハッシュテーブルに初期化(カウント0で設定)
foreach ($keyword in $searchKeywords) {
$keywordCounts[$keyword] = 0
}
# ディレクトリ内のすべてのaccessログを検索
Get-ChildItem -Path $directory -Filter access.* -Recurse | ForEach-Object {
$file = $_.FullName
# ファイル内の内容を読み込み
$lines = Get-Content -Path $file
# 除外キーワードを含まない行をフィルタリング
$filteredLines = $lines | Where-Object {
$excludeMatch = $false
foreach ($exclude in $excludeKeywords) {
if ($_ -match $exclude) {
$excludeMatch = $true
break
}
}
-not $excludeMatch
}
# フィルタリング後の行に対して各キーワードのヒット件数をカウント
foreach ($keyword in $searchKeywords) {
$count = ($filteredLines | Where-Object { $_ -match $keyword } | Measure-Object).Count
$keywordCounts[$keyword] += $count
}
}
# キーワードごとのヒット件数を出力
Write-Host "Keyword Match Counts:"
foreach ($keyword in $keywordCounts.Keys) {
Write-Host "${keyword}: $($keywordCounts[$keyword])"
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme