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

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

问题描述

我正在尝试两个日期范围之间的所有行,其日期格式如下:
date_time.strftime(%Y%m%d%H%M)
so在[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.

我可以将日志中的日期转换为(从时代起),然后使用[time1 - time2]的常规grep,但这意味着读取每行,提取时间值和然后转换等。

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 ?

谢谢!

PS:
我也可以传递类似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)(20\d{8})(?!\d)/
                      && $1 >= 201211150821 && $1 <= 201211150824'

(它找到以$开头的十位数整数$ c> 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天全站免登陆