bash:两个时间戳之间的持续时间是否超过一小时?

bash:两个时间戳之间的持续时间是否超过一小时?

我正在编写一个在 CentOS 上运行的 bash 脚本,首先获取应用程序会话开始和结束的行,然后在持续时间长于一小时的情况下输出。

日志文件中的时间戳格式为:2017-01-03T00:00:15.529596-03:00

$i 是应用程序会话 ID。


这是我到目前为止所拥有的:

for i in $(  grep 'session-enter\|session-exit' logfile | awk '{ print $5}' ); do
                        echo ""
                        echo "*****   $i   *****"
        grep 'session-enter\|session-exit' logfile | grep $i

                start=$(grep session-enter logfile | grep $i | awk '{ print $1 }' | sed 's/-03:00//g')
                end=$(grep session-exit logfile | grep $i | awk '{ print $1 }' | sed 's/-03:00//g')

                epochStart=$(date -d "$start" +%s )
                epochEnd=$(date -d "$end" +%s )

                                duration=$( date -u -d "0 $epochEnd seconds - $epochStart seconds" +"%H:%M:%S" )

                        if [ "$epochStart"="" ] || [ "$epochEnd"="" ]
                                then
                                     echo Duration: $duration
                                else
                                     continue
                        fi


done

非常感谢对此的任何帮助。

答案1

看起来您已经将开始时间和结束时间存储为自纪元以来的秒数。如果这是真的,您应该能够执行以下操作:

duration=$(( $epochEnd - $epochStart ))
[ $duration -gt 3600 ] && echo "Duration: $duration"

答案2

基于以下输入数据的假设,我编写了这段代码。

将输入文件作为脚本的参数传递

输入文件

$ cat input
2017-01-03T00:00:15.529596-03:00 session-enter TEST1
......
.......
......
2017-01-03T00:00:37.529596-03:00 session-exit TEST1
.....
.....
2017-01-03T00:00:18.529596-03:00 session-enter TEST2
......
.......
......
2017-01-03T00:03:20.529596-03:00 session-exit TEST2
...........
..........
2017-01-03T00:00:18.529596-03:00 session-enter TEST3
......
.......
......
2017-01-03T02:03:20.529596-03:00 session-exit TEST3

提取物.sh

#!/bin/bash

SESSION_LOG_FILE=${1}
SESSION_EXTRACT_FILE=/tmp/$$.txt

awk '/enter/{sname=$NF;start=$1;}/exit/ && $0~sname{end=$1;}start && end{print sname,start,end;start="";end="";}' "${SESSION_LOG_FILE}" > ${SESSION_EXTRACT_FILE}

while read SESSION_NAME SESSION_START SESSION_END
do
        echo "---------${SESSION_NAME}---------"
        echo "Start Time : ${SESSION_START}"
        echo "End   Time : ${SESSION_END}"
        START_EPOCH=${SESSION_START%-*}
        START_EPOCH=$(date -d "${START_EPOCH}" +%s)
        END_EPOCH=${SESSION_END%-*}
        END_EPOCH=$(date -d "${END_EPOCH}" +%s)
        DIFFERENCE=$((${END_EPOCH}-${START_EPOCH}))
        echo "DIFFERENCE in seconds : ${DIFFERENCE}"
done < "${SESSION_EXTRACT_FILE}"

输出

$ ./extract.sh input
---------TEST1---------
Start Time : 2017-01-03T00:00:15.529596-03:00
End   Time : 2017-01-03T00:00:37.529596-03:00
DIFFERENCE in seconds : 22
---------TEST2---------
Start Time : 2017-01-03T00:00:18.529596-03:00
End   Time : 2017-01-03T00:03:20.529596-03:00
DIFFERENCE in seconds : 182
---------TEST3---------
Start Time : 2017-01-03T00:00:18.529596-03:00
End   Time : 2017-01-03T02:03:20.529596-03:00
DIFFERENCE in seconds : 7382

检查时间秒数是否大于 3600

#!/bin/bash

SESSION_LOG_FILE=${1}
SESSION_EXTRACT_FILE=/tmp/$$.txt

awk '/enter/{sname=$NF;start=$1;}/exit/ && $0~sname{end=$1;}start && end{print sname,start,end;start="";end="";}' "${SESSION_LOG_FILE}" > ${SESSION_EXTRACT_FILE}

while read SESSION_NAME SESSION_START SESSION_END
do
        START_EPOCH=${SESSION_START%-*}
        START_EPOCH=$(date -d "${START_EPOCH}" +%s)
        END_EPOCH=${SESSION_END%-*}
        END_EPOCH=$(date -d "${END_EPOCH}" +%s)
        DIFFERENCE=$((${END_EPOCH}-${START_EPOCH}))
        if [ "${DIFFERENCE}" -gt "3600" ]
                then
                        echo "---------${SESSION_NAME}---------"
                        echo "Start Time : ${SESSION_START}"
                        echo "End   Time : ${SESSION_END}"
                        echo "DIFFERENCE in seconds : ${DIFFERENCE}"
                fi
done < "${SESSION_EXTRACT_FILE}"

相关内容