log.txt
これは、この log.txt ファイルで継続的に更新される独自のタイムスタンプ (detection_time) を持つ ID データです。ID データは予測不能な数値になります。0000 ~ 9999 の範囲で、同じ ID が log.txt に再度表示される可能性があります。
log.txt
私の目標は、シェル スクリプトを使用して、最初の出現から 15 秒以内に再び出現する ID をフィルター処理することです。誰でもこれで私を助けることができますか?
ID = 4231
detection_time = 1595556730
ID = 3661
detection_time = 1595556731
ID = 2654
detection_time = 1595556732
ID = 3661
detection_time = 1595556733
より明確にするために、log.txt
上記から、ID 3661 は最初に 1595556731 の時点で表示され、次に 1595556733 で再び表示されます。これは最初の表示からわずか 2 秒後のことです。15秒以内にIDを再発行したいという私の条件に合致しています。この ID 3661 をシェル スクリプトでフィルタリングしたい
シェル スクリプトを実行した後の出力は次のようになります。
ID = 3661
私の問題は、シェル スクリプトでプログラミング アルゴリズムを開発する方法がわからないことです。
ID_new
と ID_previous
変数を使用して試してみましたが、機能しID_previous=$(ID_new) detection_previous=$(detection_new)
ていません
input="/tmp/log.txt"
ID_previous=""
detection_previous=""
while IFS= read -r line
do
ID_new=$(echo "$line" | grep "ID =" | awk -F " " '{print $3}')
echo $ID_new
detection_new=$(echo "$line" | grep "detection_time =" | awk -F " " '{print $3}')
echo $detection_new
ID_previous=$(ID_new)
detection_previous=$(detection_new)
done < "$input"
EDIT
log.txt
実際には、データは ID、detection_time、Age、および Height を含むセット内にあります。最初にこれについて言及せずに申し訳ありません
ID = 4231
detection_time = 1595556730
Age = 25
Height = 182
ID = 3661
detection_time = 1595556731
Age = 24
Height = 182
ID = 2654
detection_time = 1595556732
Age = 22
Height = 184
ID = 3661
detection_time = 1595556733
Age = 27
Height = 175
ID = 3852
detection_time = 1595556734
Age = 26
Height = 156
ID = 4231
detection_time = 1595556735
Age = 24
Height = 184
私はAwkソリューションを試しました。結果は
4231
3661
2654
3852
4231
、log.txt 内のすべての ID です。正しい出力は次のようになります。4231
3661
このことから、Age と Height のデータは、ID と detection_time という注目データの間に挿入されるため、Awk ソリューションに影響を与える可能性があると思います。