2014-01-06 64 views
0
cat file.txt 
(
    while read line 
    do 
     initial_time=`head -n 1 $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/'` 
     initial_time_sec=`date -d "$initial_time" +%s` 
    done 
) 

我想從每一行採取的日期,如果是小於60天,我必須把它存儲在變量如何使用shell腳本讀取文件內容

回答

0
while read line ; do 
    initial_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/'` 
    initial_time_sec=`date -d "$initial_time" +%s` 
    now=`date +%s` 
    pass_time=`expr $now - $initial_time_sec` 
    limit_time=$((60 * 24 * 60)) 
    if [[ $pass_time -lt $limit_time ]]; then                                           
    echo $line 
    fi 
done <data 
+0

如何比較日期這是不到60天,而文件閱讀 – user3165241

+0

@ user3165241你能舉一些你的文件內容的例子嗎? :)它是一個日誌文件? – atupal

+0

2013-11-06 15:53:49 HB線活(1) 2013-11-07 15:53:49 heartBeatCallback啓動fireHeartBeat 2013-11-08 15:53:50 refreshPollingTime結束 2013-11-07 15:53:50 heartBeatCallback測試childpid 2013-11-07 15:53:50 heartBeatCallback結束 2013-11-07 15:53:50 HB結束回調調用 2014-01-05 16:03:51 HB線程活着(1) – user3165241