开发者

Parsing a log file with Ruby using range operator

开发者 https://www.devze.com 2022-12-20 18:19 出处:网络
I have a log file that consists of many timestamps in this fashion: [2010/02/21 11:01:25] since the log files are large, 开发者_Python百科I\'d like to use a quick Ruby excerpt to be able to see just

I have a log file that consists of many timestamps in this fashion:

[2010/02/21 11:01:25]

since the log files are large, 开发者_Python百科I'd like to use a quick Ruby excerpt to be able to see just the text between two inclusive times.. say from 11:01..11:59

Using something like

while line = gets
    puts line if line =~/start/ .. line =~ /end/
end

what's the best way to go about it?

bonus points if you can show how it's done on the command line.


aaa:dev $ cat testdates.txt 
[2010/02/21 11:01:25]
[2010/02/22 11:01:25]
[2010/02/23 11:01:25]
[2010/02/21 12:01:25]
[2010/02/21 11:31:25]
aaa:dev $ cat testdates.txt | ruby -ne "require 'date'; d = DateTime.strptime(\$_.match(/\[(.+?)\]/)[1], '%Y/%m/%d %H:%M:%S'); puts \$_ if d >= DateTime.parse('2010-02-21 11:00') && d <= DateTime.parse('2010-02-21 12:00')"
[2010/02/21 11:01:25]
[2010/02/21 11:31:25]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号