处理日志文件,从后往前(考虑日志文件比较大),逐行处理,检查每行是否包含某个字符,进行统计输出

65 阅读1分钟

要求 

统计nginx日志文件 /string/compare、/person/compare、/person/save、/person/detail、/person/panorama/recog 这几个接口某日的调用次数 

 调用方式 sh count.sh access.log 03/Jan/2025 

count.sh 脚本

#!/bin/bash

# 检查输入参数是否正确
if [ "$#" -ne 2 ]; then
  echo "Usage: $0 <log_file> <date>"
  echo "Example: $0 access.log 03/Jan/2025"
  exit 1
fi

log_file=$1
date=$2

# 检查日志文件是否存在
if [ ! -f "$log_file" ]; then
  echo "Error: Log file '$log_file' not found!"
  exit 1
fi

# 初始化计数器
string_compare_count=0
person_compare_count=0
person_save_count=0
person_detail_count=0
person_panorama_recog_count=0
total_count=0

# 从文件末尾逐行读取,直到找不到指定日期
tac "$log_file" | awk -v date="$date" '
  BEGIN {
    string_compare = 0
    person_compare = 0
    person_save = 0
    person_detail = 0
    person_panorama_recog = 0
    total = 0
  }
  $0 ~ "\\[" date {
    if (index($0, "/string/compare") > 0) string_compare++
    else if (index($0, "/person/compare") > 0) person_compare++
    else if (index($0, "/person/save") > 0) person_save++
    else if (index($0, "/person/detail") > 0) person_detail++
    else if (index($0, "/person/panorama/recog") > 0) person_panorama_recog++
    total++
  }
  $0 !~ "\\[" date { exit }
  END {
    print string_compare, person_compare, person_save, person_detail, person_panorama_recog, total
  }
' | {
  # 读取awk输出的统计结果
  read string_compare_count person_compare_count person_save_count person_detail_count person_panorama_recog_count total_count

  # 输出统计结果
  echo "Statistics for $date:"
  echo "/string/compare requests: $string_compare_count"
  echo "/person/compare requests: $person_compare_count"
  echo "/person/save requests: $person_save_count"
  echo "/person/detail requests: $person_detail_count"
  echo "/person/panorama/recog requests: $person_panorama_recog_count"
  echo "Total requests: $total_count"
}