问:对于问题的论证,假设今天是 26/03/15。我如何“累积”过去 5 天的数据。
但这些日期只是一个例子。理想情况下,我希望能够积累最后的数据X天,一旦积累起来,我会通过电子邮件将积累的信息发送出去。我会每隔一天执行一次这个过程z通过 crontabs 帮助几周。
这是代码:
awk_variables=`echo "$line" | awk -F, '( $13 == "*OUT*" )||( $13 == "*IN*" ){print $1,$5,$10,$12,$13}' my_file.csv > /directory/some_directory/non_sorted_filtered.csv`
cat /directory/some_directory/non_sorted_filtered.csv | while read line
do
awk_variables="$line"
awk_variables_array=($awk_variables)
awk_time=${awk_variables_array[0]}
awk_container_ID=${awk_variables_array[1]}
awk_scan_count=${awk_variables_array[2]}
awk_part_number=${awk_variables_array[3]}
awk_direction==${awk_variables_array[4]}
awk_part_number_edited="${awk_part_number:3}"
awk_direction_edited="${awk_direction:1}"
awk_date=@''$awk_time
date=`date -d $awk_date +"%d/%m/%Y"`
echo -e "Date:$date Scan No:$awk_scan_count Part No:$awk_part_number_edited Direction:$awk_direction_edited"
done
这是输出:
Date:19/03/2015 Scan No:12 Part No:XXXX1234567 Direction:*OUT*
Date:19/03/2015 Scan No:13 Part No:XXXX1234567 Direction:*OUT*
Date:19/03/2015 Scan No:14 Part No:XXXX1234567 Direction:*OUT*
Date:19/03/2015 Scan No:17 Part No:XXXX1234567 Direction:*OUT*
Date:19/03/2015 Scan No:18 Part No:WTTO275 Direction:*OUT*
Date:19/03/2015 Scan No:19 Part No:WTTO275 Direction:*OUT*
Date:19/03/2015 Scan No:20 Part No:WTTO275 Direction:*OUT*
Date:20/03/2015 Scan No:22 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:23 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:24 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:25 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:26 Part No:XXXX1234567 Direction:*OUT*
Date:24/03/2015 Scan No:37 Part No:WTTO523 Direction:*OUT*
Date:25/03/2015 Scan No:43 Part No:WTTO548 Direction:*OUT*
Date:25/03/2015 Scan No:44 Part No:SP-TMX6BP Direction:*OUT*
Date:25/03/2015 Scan No:45 Part No:WTTO548 Direction:*OUT*
Date:25/03/2015 Scan No:49 Part No:02102 Direction:*OUT*
Date:25/03/2015 Scan No:50 Part No:02103 Direction:*OUT*
Date:25/03/2015 Scan No:51 Part No:02118 Direction:*OUT*
Date:25/03/2015 Scan No:52 Part No:02132 Direction:*OUT*
Date:25/03/2015 Scan No:53 Part No:02133 Direction:*OUT*
Date:25/03/2015 Scan No:54 Part No:02134 Direction:*OUT*
Date:25/03/2015 Scan No:55 Part No:02135 Direction:*OUT*
Date:25/03/2015 Scan No:56 Part No:19178 Direction:*OUT*
Date:25/03/2015 Scan No:57 Part No:19179 Direction:*OUT*
所以我想发送以下数据:
Date:20/03/2015 Scan No:22 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:23 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:24 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:25 Part No:XXXX1234567 Direction:*OUT*
Date:20/03/2015 Scan No:26 Part No:XXXX1234567 Direction:*OUT*
Date:24/03/2015 Scan No:37 Part No:WTTO523 Direction:*OUT*
Date:25/03/2015 Scan No:43 Part No:WTTO548 Direction:*OUT*
Date:25/03/2015 Scan No:44 Part No:SP-TMX6BP Direction:*OUT*
Date:25/03/2015 Scan No:45 Part No:WTTO548 Direction:*OUT*
Date:25/03/2015 Scan No:49 Part No:02102 Direction:*OUT*
Date:25/03/2015 Scan No:50 Part No:02103 Direction:*OUT*
Date:25/03/2015 Scan No:51 Part No:02118 Direction:*OUT*
Date:25/03/2015 Scan No:52 Part No:02132 Direction:*OUT*
Date:25/03/2015 Scan No:53 Part No:02133 Direction:*OUT*
Date:25/03/2015 Scan No:54 Part No:02134 Direction:*OUT*
Date:25/03/2015 Scan No:55 Part No:02135 Direction:*OUT*
Date:25/03/2015 Scan No:56 Part No:19178 Direction:*OUT*
Date:25/03/2015 Scan No:57 Part No:19179 Direction:*OUT*
注意:每天的扫描次数会有所不同。有时它可能是 100(例如)有时它会是 0。
答案1
这更像是一个提示,而不是解决方案。如果我没记错的话,您可以使用ISO 8601
日期格式和string compare
。类似这样的方法可能对您有用:
iso_moment=$(date -u +"%Y-%m-%dT%H:%M:%SZ" -d "-5 days")
iso_date=$(date -d $awk_date +"%Y-%m-%dT%H:%M:%SZ")
if [[ "${iso_date}" > "${iso_moment}" ]]
then
echo -e "Date:$date Scan No:$awk_scan_count Part No:$awk_part_number_edited Direction:$awk_direction_edited"
fi