Filter with the Time in LINUX

2019-08-17 08:15发布

I have text file that containing two columns and im trying to write a linux code to filter data. The data has "pass" & "fail" status. I need to Show all "pass" lines , when previous line is "fail" and both lines should be within the 30 second of time. pls help me to do this job. thank you. below shows my text file

12/3/2017 13:25:16 AM    fail
12/3/2017 13:25:35 AM    pass
12/3/2017 14:55:11 AM    pass
12/5/2017 23:46:31 AM    pass
12/7/2017 13:15:35 AM    pass
12/7/2017 19:25:51 AM    pass
12/1/2017 15:39:09 AM    fail
12/1/2017 15:39:20 AM    pass
12/9/2017 21:25:45 AM    pass
12/5/2017 16:25:51 AM    pass

my required result

12/3/2017 13:25:35 AM    pass
12/1/2017 15:39:20 AM    pass

1条回答
仙女界的扛把子
2楼-- · 2019-08-17 08:48

Extended GNU awk solution:

awk 'function get_time(d_str){ 
         split(d_str, d, /[/:[:space:]]/); 
         return mktime(sprintf("%d %d %d %d %d %d",d[3],d[1],d[2],d[4],d[5],d[6])) 
     }
     $4=="pass" && status=="fail" && (get_time(prev_date)-get_time($1" "$2))<=30;
     { prev_date=$1" "$2; status=$4 }' file

The output:

12/3/2017 13:25:35 AM    pass
12/1/2017 15:39:20 AM    pass

  • function get_time(d_str){ ... } - returns timestamp converted from d_str(datetime string)
查看更多
登录 后发表回答