Python 示例

from collections import Counter

counter = Counter()
with open('cdn.log', 'r', encoding='utf-8') as f:
    for line in f:
        if ' HIT ' in line:
            counter['HIT'] += 1
        elif ' MISS ' in line:
            counter['MISS'] += 1

total = counter['HIT'] + counter['MISS']
ratio = (counter['HIT'] / total * 100) if total else 0
print({'total': total, 'hit_ratio': round(ratio, 2)})