gpt4 book ai didi

regex - 解析日志文件,检查日期,报告结果

转载 作者:可可西里 更新时间:2023-11-01 11:46:23 26 4
gpt4 key购买 nike

我需要获取打印在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.datBefore ftp connectionAfter 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

最佳答案

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

$ awk '{print $5,($0~"After.*"d?"PASS":"FAIL")}' d="$(date +'%a %b %d')" RS= file
file1.dat FAIL
file2.dat PASS

关于regex - 解析日志文件,检查日期,报告结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19544030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com