将您的最小/最大日期转换为“自纪元以来的秒数”,
MIN=`date --date="$1" +%s`
MAX=`date --date="$2" +%s`
将n
每个日志行中的第一个单词转换为相同的,
L_DATE=`echo $LINE | awk '{print $1 $2 ... $n}'`
L_DATE=`date --date="$L_DATE" +%s`
比较并丢弃线条,直到达到MIN
,
if (( $MIN > $L_DATE )) ; then continue ; fi
比较并打印行,直到达到MAX
,
if (( $L_DATE <= $MAX )) ; then echo $LINE ; fi
超过时退出MAX
。
if (( $L_DATE > $MAX )) ; then exit 0 ; fi
整个脚本minmaxlog.sh看起来像这样,
#!/usr/bin/env bash
MIN=`date --date="$1" +%s`
MAX=`date --date="$2" +%s`
while true ; do
read LINE
if [ "$LINE" = "" ] ; then break ; fi
L_DATE=`echo $LINE | awk '{print $1 " " $2 " " $3 " " $4}'`
L_DATE=`date --date="$L_DATE" +%s`
if (( $MIN > $L_DATE )) ; then continue ; fi
if (( $L_DATE <= $MAX )) ; then echo $LINE ; fi
if (( $L_DATE > $MAX )) ; then break ; fi
done
我在这个文件minmaxlog.input上运行它,
May 5 12:23:45 2009 first line
May 6 12:23:45 2009 second line
May 7 12:23:45 2009 third line
May 9 12:23:45 2009 fourth line
June 1 12:23:45 2009 fifth line
June 3 12:23:45 2009 sixth line
像这样,
./minmaxlog.sh "May 6" "May 8" < minmaxlog.input