0

いくつかのログをリアルタイムで分析し、数秒ごとに前回読み終えたファイル内の場所を見つけなければならないという事実にどのようにアプローチするかを考えて、bash で簡単なスクリプトを書いています。今、私は次のようなことをしています:

LOG_FILE=path_to_file
DELAY=1   #time between refresh
LINES=100 #lines to read at one cycle

LAST=$(tail -n 1 $LOG_FILE)      

IFS=$'\n'
while true;
do
    clear;
    found=0
    LOG=$(tail -n $LINES $LOG_FILE)
    for line in $LOG
    do
        if [ $line = $LAST ]; then 
            found=1
            continue
        fi        
        if [ $found = 0 ]; then
            continue
        fi
        #Analyzing counting nd stuff.
        echo "$stuff"
    done
    LAST=$line
    sleep $DELAY;
done

したがって、すべてのサイクルで、ファイルの終わりからいくつかの行を取得し、前回の実行で最後だった行を探しています。これは、1 サイクルで定義された数の行が追加されるまでは問題なく機能します。私はいつでも次のようなことを言うことができますLINES=10000が、この場合、前の実行から最後の行をまだ見つけたかどうかを判断するためだけに、無用な実行が無数にあります。もう少し効率的にできるかどうか疑問に思っていますか?

4

1 に答える 1

1

次のような sth を探していると思います。

#!/bin/bash
GAP=10     #How long to wait
LOGFILE=$1 #File to log to

if [ "$#" -ne "1" ]; then
    echo "USAGE: `basename $0` <file with absolute path>"
    exit 1
fi


#Get current long of the file
len=`wc -l $LOGFILE | awk '{ print $1 }'`
echo "Current size is $len lines."

while :
do
    if [ -N $LOGFILE ]; then
        echo "`date`: New Entries in $LOGFILE: "
        newlen=`wc -l $LOGFILE | awk ' { print $1 }'`
        newlines=`expr $newlen - $len`
        tail -$newlines $LOGFILE
        len=$newlen
    fi
sleep $GAP
done
exit 0
于 2014-02-25T11:07:56.273 に答える