ログファイル:「MMM DD HH:MM:SS」より古いエントリのみを処理します。

ログファイル:「MMM DD HH:MM:SS」より古いエントリのみを処理します。

私のログファイルを処理する必要があります/var/log/mylogs.log。私は新しいエントリだけに興味がありますが、Jul 20 15:00:00ログには以前のエントリも含まれているからです。

cat特定の日付より古い項目だけを入力する簡単な方法はありますか?

ベストアンサー1

日付を解析するための小さなスクリプトを書くことができますbashdate

#!/usr/bin/env bash

## Your date threshold
limit="Jul 20 15:00:00"
## Your limit in seconds since the UNIX epoch
limit_seconds=$(date -d "$limit" +%s)

while read line; do
    ## Extract the date and convert to seconds since epoch for comparison
    date=$(date -d "$(echo "$line" | cut -d ' ' -f -4)" +%s);
    ## Is this newer than the limit? If yes, print the line
    if [ "$date" -ge "$limit_seconds" ]; then
    echo "$line"
    fi

done

このスクリプトをパス(たとえば/usr/local/bin/parse_log.sh)に保存してから、次のように実行できます。

parse_log.sh < /var/log/mylogs.log

おすすめ記事