首页 > 解决方案 > 删除日志文件中特定日期之前的行

问题描述

我有一个 ffmpeg 进程日志文件,其中每天都会附加日志数据。我想通过查看日志并删除属于某个日期的行来自动维护此日志。我尝试了一些东西,但没有帮助。

我该怎么做?请帮忙。谢谢。

例子。删除 6 月 1 日之前的所有行。

#!/bin/bash

var=`date -d "now-30 day" | awk '{print $3}'`
path="/home/pi/scripts/"

## Removing Entries before 30 Days ####
for logs in $path/process.log; do

awk -v rot=$var '{ if ($NF > rot ) {print $0}}' $logs > $path/temp.log
mv $path/temp.log $logs

done

包含 6 月之前的数据的 process.log 文件示例我想删除所有以前的数据。

Oct 28 06:48:15 #####################################Start Time########################################

Stopping Transaction Video Recording
ffmpeg version 4.2.1-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-6 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libfribidi --enable-libass --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libxml2 --enable-libxvid --enable-libzimg
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
/dev/v4l/by-id/usb-WN-720P-HD_WN-720P-HD_WN-720P-HD-video-index0: Device or resource busy

Oct 28 06:48:17 #####################################End Time########################################


Oct 29 07:38:13 #####################################Start Time########################################

Stopping Transaction Video Recording
ffmpeg version 4.2.1-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-6 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libfribidi --enable-libass --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libxml2 --enable-libxvid --enable-libzimg
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
/dev/v4l/by-id/usb-WN-720P-HD_WN-720P-HD_WN-720P-HD-video-index0: Device or resource busy

Oct 29 07:38:13 #####################################End Time########################################"

这是我要保留的数据,属于当前月份的数据:

Jun 02 09:49:08 #####################################Start Time########################################

Stopping Transaction Video Recording
ffmpeg version 4.2.1-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-6 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libfribidi --enable-libass --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libxml2 --enable-libxvid --enable-libzimg
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
[video4linux2,v4l2 @ 0x36e75e0] The driver changed the time per frame from 1/5 to 1/30
[mjpeg @ 0x36e8530] EOI missing, emulating
Input #0, video4linux2,v4l2, from '/dev/v4l/by-id/usb-WN-720P-HD_WN-720P-HD_WN-720P-HD-video-index0':
  Duration: N/A, start: 13731.683520, bitrate: N/A
    Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mpeg4 (native))
Press [q] to stop, [?] for help

标签: linuxbashshellscriptinglinuxthreads

解决方案


我们错过了编码防水的元素。如果可以在日志文件中找到过去几年的条目,我们需要知道 ao,因为日志文件没有提及条目的年份,只有月份和日期。

我们假设一个明显在未来的日期对应于前几年的条目。如果您在 1 月初运行该脚本,您将删除 12 月小于 30 天的条目。

这意味着如果没有更多的知识,就无法以完全令人满意的方式解决问题。有时日志文件的名称可以包含一些关于我们在这里忽略的年份的有用信息。

无论如何,下面的解决方案可能有效,但不是非常理想:

#!/bin/bash

#time stamp older entries to keep
ts_now=$(date +"%s" -d now)
ts_oldest_to_keep=$(date +"%s" -d "now - 30 days")
#adapt path to point to the directory where log files are living
path="."

for ts in $(grep "Start Time" "$path/"*".log" | awk '{print $1, $2}' | date +"%s" -f - | sort -u)
do 
  # get the timestamp corresponding to all the entries to be removed
  if [ $ts -lt $ts_oldest_to_keep ] || [ $ts -gt $ts_now ]
  then 
    # convert the timestamp into the string "%b %d" i.e. the abbreviated month 
    # name in English followed by space and zero padded day of month.
    # LC_ALL=C to make sure you get an English name of month
    ym=$(LC_ALL=C date +"%b %d" -d "@$ts") 
    # delete the block of lines beginning with "$ym" and ending with 
    # the same string
    sed -i "/^$ym.*Start Time/,/^$ym.*End Time/d" "$path/"*".log"
  fi
done

您首先从日志文件中获取所有可能的日期。您可以将日期转换为时间戳,以便对日期进行数字比较,并使用 sed 删除行块,而无需临时文件。

也应该可以一次性在 AWK 中寻找解决方案,但 AWK 缺少一个标准函数,可以轻松地将日期转换为Jun 01.

无论如何,在搞砸之前备份您的日志文件。


推荐阅读