previousDay=`date +"%Y-%m-%d" -d "-1 day"`
finish_time1=$(grep -o -m1 "$previousDay [0-9][0-9]:[0-9][0-9]:[0-9][0-9]" sample.txt)
在这里,我试图找出提供日期的前一个日期。这里是sample.txt内容
2013-11-07 15:53:49 HB Thread alive (1)
2013-11-07 15:53:49 heartBeatCallback starting fireHeartBeat
2013-11-07 15:53:50 refreshPollingTime END
2013-11-07 15:53:50 heartBeatCallback testing childpid
2014-01-24 15:53:50 heartBeatCallback end
2014-01-25 15:53:50 HB end callback call
2014-01-25 15:53:50 uploadTSLog TS_LOG_FILE is /public/tmp/testomatic/ts_logs/2013-11- 07_08-21-19_ONEOS90-VOIP_SIP_11N_FT-V5.1R5E12_NB83163_FT4_T10_112741.log
2014-01-27 15:53:50 uploadTSLog: no new logs to upload
2014-01-27 15:54:50 HB going to sleep */
答案 0 :(得分:1)
[编辑2:尝试回答真实问题:在给定日期之前,找到该日期之前的最后一个日志...]
Date="2014-01-24"
awk -v d=$Date '
($1 < d) { previous=$0 ; }
($1 >=d) { print previous ; exit ; }
' sample.txt
此输出:
2013-11-07 15:53:50 heartBeatCallback testing childpid
====下面:由于历史原因,我之前的回答,认为你想要某个特定日期的“前一天”... ====
[编辑:这是第1天,而不是第2天。通过另外60 * 60 * 24秒]轻松修改第2天
您需要从日期中找到“第-1天”。
我让你做一些“男人约会”,所以我给你的程序,而不是确切的方法
只是
答案 1 :(得分:0)
finish_time=0
previousDay=`date +"%Y-%m-%d" -d "-1 day"`
if [ $finish_time -eq 0 ]; then
tac logfile.txt |
(
while read line
do
finish_time=`echo $line | sed -e 's/\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9] [0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1/'`
file_content_date=`date -d "$finish_time" +%Y%m%d`
comparison_prev_date=`date -d "$previousDay" +%Y%m%d`
if [ $comparison_prev_date -ge $file_content_date ]; then
comparison_end_date=$(date -d "$file_content_date" +%Y%m%d)
break
fi
完成 ) 网络