로그 파일에서 시간별 데이터 계산

로그 파일에서 시간별 데이터 계산

로그 파일에서 매시간 레코드 수를 가져오고 싶습니다. 샘플 데이터는 다음과 같습니다.

001:2017-05-23 00:00:01 002:10.10.10.63
001:2017-05-23 00:00:03 002:10.10.10.63
001:2017-05-23 00:00:05 002:10.10.10.63
001:2017-05-23 00:00:07 002:10.10.10.63
001:2017-05-23 00:00:09 002:10.10.10.63
001:2017-05-23 01:00:12 002:10.10.10.63
001:2017-05-23 01:00:14 002:10.10.10.63

위에서 데이터 출력은 다음과 같아야 합니다.

00 = 5
01 = 2

답변1

cut -f2 -d' ' logfile
| cut -f1 -d:
| sort
| uniq -c
| sed 's/ *\([0-9]\+\) \([0-9][0-9]\)/\2 = \1/'
| sort
  1. 시간만 출력합니다.
  2. 시간만 추출합니다.
  3. 출력 정렬 - 다음 줄에 필요
  4. 매 시간마다 발생 횟수를 계산하고 출력합니다.count hour
  5. 서식을 수정합니다
  6. 시간별로 정렬

답변2

find아래와 같이 속성 과 함께 사용할 수 있습니다 -printf. 저는 이것을 일상적인 작업에 사용했습니다.

find /path/ -type f -printf '%TY-%Tm-%Td-%TH\n' | sort | uniq -c

답변3

많은 솔루션이 있으며 그 중 하나는 다음과 같습니다.

log_file=/var/log/messages                        # log file for extract
d=2022-10-28                                      # start date
while [ "$d" != 2022-11-04 ]; do                  # loop for date range
  echo $d                                         # echo ACTUAL date
  for h in {00..24}; do                           # loop for hours
    act=$(date -d "$d" +'%b %d')" $h:"            # create date for ACTUAL date in requested format: %b return month in Jan Feb..., %d return month number
    echo $act                                     # Print actual hour
    grep "^$act" $log_file                        # grep $ACT from beginning of line of log_file and count lines
  done
  d=$(date -I -d "$d + 1 day")                    # add +1 day for start date to the main loop
done 

관련 정보