在LINUX中用时间过滤 [英] Filter with the Time in LINUX

查看:115
本文介绍了在LINUX中用时间过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的文本文件,我试图编写一个Linux代码来过滤数据.数据具有通过"& 失败"状态.我需要显示所有通过"行,当上一行是失败"并且两行都应在30秒的时间内.请帮助我完成这项工作.谢谢你.下面显示了我的文本文件

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

我所需的结果

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

推荐答案

扩展的GNU awk 解决方案:

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

输出:

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


  • function get_time(d_str){ ... }-返回从d_str( datetime 字符串)
  • 转换的时间戳


    • function get_time(d_str){ ... } - returns timestamp converted from d_str(datetime string)
    • 这篇关于在LINUX中用时间过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆