grep 大于指定日期的行,日志中的日期范围之间的grep

博主寻求在Unix中查找特定日期区间(如201211150821-201211150824)的日志行,分享了一个Perl解决方案,通过正则表达式匹配整数时间戳,避免逐行解析。这个方法可以快速定位符合条件的行,减少不必要的数据处理。
摘要由CSDN通过智能技术生成

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 *.log | **grep [201211150821 - 201211150824]**

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

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 .

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

Thanks!

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.

解决方案

A Perl solution:

perl -wne 'print if m/(?

&& $1 >= 201211150821 && $1 <= 201211150824'

(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.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值