0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スクリプト20241118_リクエストカウント

Posted at
# 検索するディレクトリのパス
$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])"
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?