日志中日期范围之间的 grep [英] grep between date ranges in a log

查看:19
本文介绍了日志中日期范围之间的 grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 grep 2 个日期范围之间的所有行,其中日期的格式如下:date_time.strftime("%Y%m%d%H%M")所以说 [201211150821 - 201211150824]

I am trying to grep all the lines between 2 date ranges , where the dates are formatted like this : date_time.strftime("%Y%m%d%H%M") so say between [201211150821 - 201211150824]

我正在尝试编写一个脚本,其中涉及查找这些日期之间的行:

I am trying to write a script which involves looking for lines between these dates:

cat <somepattern>*.log | **grep [201211150821 -  201211150824]** 

我试图找出 unix 中是否存在可以查找日期范围的内容.

I am trying to find out if something exists in unix where I can look for a range in date.

我可以将日志中的日期转换为(自纪元以来),然后将常规 grep 与 [time1 - time2] 一起使用,但这意味着读取每一行,提取时间值,然后将其转换等.

I can convert dates in logs to (since epoch) and then use regular grep with [time1 - time2] , but that means reading each line , extracting the time value and then converting it etc .

可能已经存在一些简单的东西,以便我可以指定日期/时间戳范围,就像我可以为 grep 提供数字范围一样?

May be something simple already exist , so that I can specify date/timestamp ranges the way I can provide a numeric range to grep ?

谢谢!

附:我也可以传入类似于 2012111511(27|28|29|[3-5][0-9]) 的模式,但这特定于我想要的范围,每次尝试不同的日期都很乏味,而且变得更加棘手在运行时执行.

P.S: Also I can pass in the pattern something like 2012111511(27|28|29|[3-5][0-9]) , but thats specific to ranges I want and its tedious to try out for different dates each time and gets trickier doing it at runtime.

推荐答案

Perl 解决方案:

perl -wne 'print if m/(?<!d)(20d{8})(?!d)/
                      && $1 >= 201211150821 && $1 <= 201211150824'

(它查找以 20 开头的第一个十位整数,如果该整数在您感兴趣的范围内,则打印该行.如果找不到任何这样的整数,则跳过行.您可以调整正则表达式,以更加严格地限制有效月份和小时数等.)

(It finds the first ten-digit integer that starts with 20, and prints the line if that integer is within your range of interest. If it doesn't find any such integer, it skips the line. You can tweak the regex to be more restrictive about valid months and hours and so on.)

这篇关于日志中日期范围之间的 grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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