AWKを使用する:今日から5日前までの日付を含む行を検索するための印刷フィールドを定義する方法は?

AWKを使用する:今日から5日前までの日付を含む行を検索するための印刷フィールドを定義する方法は?

このリストをまとめて、5日間のデータ(今日から5日前まで)のみを含めようとしています。試してみawkましたが役に立たなかった。 awkに固執する必要もありません。出力は次のとおりです。

awk日付を継続的に検索するコードは次のとおりです。today's

awk -F " " -v todate="$(date +%Y-%m-%d)" 'todate' output.txt

これは私のwhileループコードです。

while read line
    do
    if (("$(date +%Y-%m-%d)" in $line))
        then
            echo $line
done < output.txt

output.txtファイルは次のとおりです。

2018-05-09 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 16:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 05:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 18:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 07:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 20:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 09:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 22:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-14 11:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 00:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;swlt;;BKP_USGSN03_OLP_MK8;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau;;2025,licensed_sau_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau_lte;;1025,licensed_sau_lte_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;pdp;;2001,licensed_pdp_normal;CXS101289_R73B13(001-00-12)
2018-05-15 13:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 02:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 15:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 04:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 17:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)

ベストアンサー1

次のスクリプトでは、関数はmktimeファイルの最初と2番目のフィールドに基づいてunixタイムスタンプを生成します。これは、datetoパラメータで指定された参照日付と比較できますawk

awk -v startdate=$(date -d '5 days ago' +%s) '{d=$1 OFS $2; gsub("[-:]", " ", d); t=mktime(d)} t>startdate' file

おすすめ記事