如何根据时间条件在bash中拆分日志文件 [英] How to split log file in bash based on time condition

查看:46
本文介绍了如何根据时间条件在bash中拆分日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有时间戳的简单日志文件,以毫秒为单位:

I have simple log file with timestamp in milliseconds like this one:

[02/03/2020 07:53:58.859000] 5
[02/03/2020 07:53:58.935300] 2
[02/03/2020 10:04:50.355600] 0
[02/03/2020 10:04:51.028900] 1
[02/03/2020 10:38:13.468200] 6

我想获得更好的可读性,因此+ -2秒以这样的破折号分隔日志:

And I want to achieve better readability so +-2seconds separate logs by dashes like this one:

[02/03/2020 07:53:58.859000] 5
[02/03/2020 07:53:58.935300] 2
------------------------------
[02/03/2020 10:04:50.355600] 0
[02/03/2020 10:04:51.028900] 1
------------------------------
[02/03/2020 10:38:13.468200] 6

如何通过bash脚本中的简单循环来实现它?到目前为止,我已经弄清楚如何从字符串 NEW_VALUE1 ="$(date -d" $ VALUE 2 seconds"+'%d/%m/%Y%H:%M:%S'格式化和修改日期)",但没有运气将其实现为功能性结果.

How to achieve it by simple loop in bash script? So far I figured out how to format and modify date from string NEW_VALUE1="$(date -d "$VALUE 2 seconds" +'%d/%m/%Y %H:%M:%S')" but with no luck to implement it to functional result.

推荐答案

使用GNU awk :

awk -F'[[/:. ]' '
  { t=mktime($4" "$3" "$2" "$5" "$6" "$7) }
  NR>1 && t>tlast+2 { print "------------------------------" }; 1
  { tlast=t }
' file

  • 使用 [/: ..和空格字符作为字段分隔符,并创建时间戳记每行 t .
  • 如果不是第一行并且 t> ;,则打印分隔线.tlast + 2 .
  • 打印当前行.
  • t 的值分配给 tlast .
    • Use [, /, : . and the space character as field separator characters and create a timestamp t for each line.
    • Print a separator line if this is not the first line and if t > tlast + 2.
    • Print the current line.
    • Assign value of t to tlast.
    • 这篇关于如何根据时间条件在bash中拆分日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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