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?

summarize_license_usage

Posted at
#!/bin/bash

# 引数チェック
if [ $# -ne 1 ]; then
    echo "Usage: $0 <logfile>"
    exit 1
fi

INPUT_FILE="$1"

# ファイル存在チェック
if [ ! -f "$INPUT_FILE" ]; then
    echo "Error: File '$INPUT_FILE' not found."
    exit 2
fi

# 一時ファイル作成
TMPFILE=$(mktemp)

# awkで集計して一時ファイルに出力(ヘッダー含む)
awk -v outfile="$TMPFILE" '
{
    split($1, date_parts, "-")
    date = date_parts[1] "-" date_parts[2] "-" date_parts[3]

    match($0, /idx="[^"]+"/, idx_arr)
    if (idx_arr[0] != "") {
        idx = substr(idx_arr[0], 6, length(idx_arr[0]) - 6)
    }

    match($0, /b=[0-9]+/, b_arr)
    if (b_arr[0] != "") {
        split(b_arr[0], b_parts, "=")
        b = b_parts[2]
    }

    usage[date][idx] += b
    idx_set[idx] = 1
    date_set[date] = 1
}

END {
    # ヘッダー行を一時ファイルに出力(インデックス名はそのまま)
    line = "\"Date\""
    for (i in idx_set) {
        line = line "," "\"" i "\""
        idx_list[++idx_count] = i
    }
    print line > outfile

    # 日付ごとのデータを一時ファイルに出力(すべてダブルクォート付き)
    for (d in date_set) {
        line = "\"" d "\""
        for (j = 1; j <= idx_count; j++) {
            i = idx_list[j]
            formatted = sprintf("%'\''d", usage[d][i] + 0)
            line = line "," "\"" formatted "\""
        }
        print line >> outfile
    }
}
' "$INPUT_FILE"

# ヘッダーを抽出
HEADER=$(head -n 1 "$TMPFILE")

# ソートして出力
echo "$HEADER"
tail -n +2 "$TMPFILE" | sort

# 一時ファイル削除
rm -f "$TMPFILE"
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?