如何使用Ruby打印包含特定关键字的特定行的日期和时间?

时间:2021-05-02 16:00:55

In other words i need Ruby to read from the attached log file and report the dates and time that has the keyword MAY_DAY. I am able to print out all the information but I don't have the slightest idea on how to print out the specific entries.

换句话说,我需要Ruby从附加的日志文件中读取并报告具有关键字MAY_DAY的日期和时间。我能够打印出所有信息,但我对如何打印出特定条目没有任何想法。

I am an uber noob and find ruby extremely difficult to understand. I appreciate all help and respectful criticism. Thanks

我是一个超级菜鸟,发现红宝石极难理解。我感谢所有的帮助和尊重的批评。谢谢

test.txt

Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY 
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY 
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE 
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH 
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY 
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN

RestartMonitor.rb

class RestartMonitor    

   counter = 1
   begin
      file = File.new("test.txt", "r")
      while (line = file.gets)
         puts "#{counter}: #{line}"
         counter = counter + 1
      end

 end

When i run the file i get the following results:

当我运行该文件时,我得到以下结果:

Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY 
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY 
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE 
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH 
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY 
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN

When i run the code i would like it to only display the date and times that have the keyworld MAY_DAY. so the output should be:

当我运行代码时,我希望它只显示具有keyworld MAY_DAY的日期和时间。所以输出应该是:

Oct 15 12:54:01
Oct 16 14:16:09
Oct 19 05:17:58

1 个解决方案

#1


1  

One way to do it would be like so (within a block that's iterating over the lines in the file, obviously):

一种方法就是这样(显然在一个块中迭代文件中的行):

if line.include?('MAY_DAY')
  puts line[0..14]
end

Since the date information (which is what you want output) appears in the same position and is the same length in every line, we don't bother doing any parsing of the text for the output - just spit out the first 15 characters.

由于日期信息(您想要输出的信息)出现在相同的位置并且每行中的长度相同,因此我们不打算对输出进行任何文本解析 - 只需吐出前15个字符。

I'm tempted to try to compress all of this into a single regular expression, but this ought to work. Obviously, you could do something other than print out the date within the conditional, and if you wanted to work with it as a date, you could pass it to DateTime.parse() (just remember to require 'date' first).

我很想尝试将所有这些压缩成一个正则表达式,但这应该有效。显然,你可以做一些其他事情,而不是在条件中打印出日期,如果你想把它当作日期,你可以把它传递给DateTime.parse()(只记得首先要求'date')。

#1


1  

One way to do it would be like so (within a block that's iterating over the lines in the file, obviously):

一种方法就是这样(显然在一个块中迭代文件中的行):

if line.include?('MAY_DAY')
  puts line[0..14]
end

Since the date information (which is what you want output) appears in the same position and is the same length in every line, we don't bother doing any parsing of the text for the output - just spit out the first 15 characters.

由于日期信息(您想要输出的信息)出现在相同的位置并且每行中的长度相同,因此我们不打算对输出进行任何文本解析 - 只需吐出前15个字符。

I'm tempted to try to compress all of this into a single regular expression, but this ought to work. Obviously, you could do something other than print out the date within the conditional, and if you wanted to work with it as a date, you could pass it to DateTime.parse() (just remember to require 'date' first).

我很想尝试将所有这些压缩成一个正则表达式,但这应该有效。显然,你可以做一些其他事情,而不是在条件中打印出日期,如果你想把它当作日期,你可以把它传递给DateTime.parse()(只记得首先要求'date')。