3

我需要把打印的时间戳记After FTP connection下来,看看今天是否发生了。

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

---------------------------------------------------------------------
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.datOR Before ftp connectionOR的所有信息After ftp connection。考虑到上面的示例,我得到 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比较并打印结果FAIL

4

3 回答 3

3

正确定义记录会使事情变得容易得多:

$ awk '{print $5,($0~"After.*"d?"PASS":"FAIL")}' d="$(date +'%a %b %d')" RS= file
file1.dat FAIL
file2.dat PASS
于 2013-10-23T14:53:08.813 回答
2

使用 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 的片段,不过感谢所有三个冠军。

于 2013-10-23T14:49:45.000 回答
1

这很棘手!

$ awk -vtoday=$(date "+%Y%m%d")
   '/^Opening/ {file=$4}
    /^After  ftp connection/ 
         {$1=$2=$3=$4=$5=$6=$NF="";
          r="date -d \"" $0 "\" \"+%Y%m%d\""; r | getline dat;
          if (today==dat) {print file, "PASS"} 
          else {print file, "FAIL"}}
 ' file
For file1.dat FAIL
For file2.dat PASS

解释

  • -vtoday=$(date "+%Y%m%d")以“20131023”格式给出今天的日期
  • /^Opening/ {file=$4}获取以文件名开头的行Opening并存储文件名,该文件名恰好位于第 4 个字段中。
  • /^After ftp connection/在以“ftp 连接后...”开头的行上,执行以下操作:
  • {$1=$2=$3=$4=$5=$6=$NF="";最多删除第 6 个字段和最后一个字段,剩下的就是日期信息。
  • r="date -d \"" $0 "\" \"+%Y%m%d\""; r | getline dat;计算该行的 YYYYMMDD 格式的日期。
  • if (today==dat) {print file, "PASS}比较日期。
  • else {print file, "FAIL"}同上。
于 2013-10-23T14:40:27.057 回答