正则表达式 – 解析日志文件,检查日期,报告结果

我需要在FTP连接后打印时间戳并检查它是否发生在今天.

我有一个日志文件,其中包含以下内容:

---------------------------------------------------------------------
Opening connection for file1.dat 
---------------------------------------------------------------------
---------------------------------------------------------------------
Before ftp connection -- time is -- Mon Oct 21 04:01:52 CEST 2013 
---------------------------------------------------------------------
---------------------------------------------------------------------
After  ftp connection -- time is Mon Oct 21 04:02:03 CEST 2013 .
---------------------------------------------------------------------
---------------------------------------------------------------------
Opening connection for file2.dat
---------------------------------------------------------------------
---------------------------------------------------------------------
Before ftp connection -- time is -- Wed Oct 23 04:02:03 CEST 2013 
---------------------------------------------------------------------
---------------------------------------------------------------------
After  ftp connection -- time is Wed Oct 23 04:02:04 CEST 2013 .
---------------------------------------------------------------------

期望的输出:

INPUT:file1.dat --> FAIL # since it is Oct 21st considering today is Oct 23.
INPUT:file2.dat --> PASS # since it is Oct 23rd.
INPUT:file3.dat --> FAIL # File information does not exist

到目前为止我尝试了什么:

grep "file1.dat\\|Before ftp connection\\|After  ftp connection" logfilename

但是这会返回与file1.dat匹配的所有信息或在ftp连接之前或在ftp连接之后.考虑到上面的示例,我得到5行,其中最后2行来自file2.dat:

Opening connection for file1.dat 
Before ftp connection -- time is -- Mon Oct 21 04:01:52 CEST 2013 
After  ftp connection -- time is Mon Oct 21 04:02:03 CEST 2013 .
Before ftp connection -- time is -- Wed Oct 23 04:02:03 CEST 2013 
After  ftp connection -- time is Wed Oct 23 01:02:04 CEST 2013 .

我被困在这里.理想情况下,我需要采取Mon Oct 21 04:02:03 CEST 2013并比较并打印结果失败.

最佳答案
使用awk:

# read dates in shell variables
read x m d x x y < <(date)

awk -v f='file2.dat' -v m=$m -v d=$d -v y=$y '$0 ~ f {s=1; next} 
   s && /After  ftp connection/ {
       res = ($8==m && $9==d && $12==y) ? "PASS" : "FAIL";
       print f, res; exit
   }' file.log

file2.dat PASS

由OP跟进:

我通过这个实现了预期的结果:

check_success () 
{

    CHK_DIR=/Archive

    if [[ ! -d ${CHK_DIR} ]]; then 
        exit 1
    elif [[ ! -d ${LOG_FOLDER} ]]; then 
        exit 1
    fi

    count_of_files=$(ls -al --time-style=+%D $CHK_DIR/*.dat  | grep $(date +%D) | cut -f1 | awk '{ print $7}' | wc -l)

    if [[ $count_of_files -lt 1 ]]; then 
        exit 2
    fi

    list_of_files=$(basename $(ls -al --time-style=+%D $CHK_DIR/*.dat  | grep $(date +%D) | cut -f1 | awk '{ print $7}'))

    for filename in $list_of_files
    do
        filename=basename filename
        lg_name=$(grep -El "Opening.*$filename" $LOG_FOLDER/* | head -1 )
        m=$(date +%b)
        d=$(date +%d)
        y=$(date +%Y)
        output=$(awk -v f=$filename -v m=$m -v d=$d -v y=$y '$0 ~ f {s=1; next} s && /After  ftp connection/ { res = ($8==m && $9==d && $12==y) ? "0" : "1"; print res; exit }' $lg_name)

        if [[ ${output} != 0 ]]; then
            exit 2
        fi
    done
    exit 0
}

我使用了Anubhava的片段,但感谢所有三位冠军.

转载注明原文:正则表达式 – 解析日志文件,检查日期,报告结果 - 代码日志