Solved

How do i print the date and time of specific lines that contain a certain key word using Ruby?

Posted on 2013-10-28
1
304 Views
Last Modified: 2013-10-28
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.

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:

Oct 15 12:54:01
Oct 16 14:16:09
Oct 19 05:17:58
0
Comment
Question by:djphillovesyou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39607119
You'll want Ruby's version of a substring search:

if line['MAY_DAY']

Open in new window


And then to parse the date out, you can use the same functionality, but pass a regex instead:

line[/[A-Za-z]+ +\d+ +\d+:\d+:\d+/]

Open in new window


The regex meaning:

/         - Pattern delimiter
[A-Za-z]  - One or more ( + ) of any letter ( [A-Za-z] )
 +        - One or more ( + ) spaces
\d+       - One or more ( + ) of any digit ( \d )
 +        - One or more ( + ) spaces
\d+       - One or more ( + ) of any digit ( \d )
:         - Literal colon
\d+       - One or more ( + ) of any digit ( \d )
:         - Literal colon
\d+       - One or more ( + ) of any digit ( \d )
/         - Pattern delimiter

Open in new window


Putting it all together:

class RestartMonitor
    counter = 1
    
    begin
        file = File.new("test.txt", "r")
        while (line = file.gets)
            if line['MAY_DAY']
                date = line[/\w+ +\d+ +\d+:\d+:\d+/]
                puts "#{counter}: #{date}"
                counter = counter + 1
            end        
        end
    end
end

Open in new window


Screenshot
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Attempting to install LESS on Windows 7 with Ruby Installer 11 755
Choosing a Server-Side Scripting Language - Criterion 5 5 298
Ruby cannot load file problem 7 586
Github help 2 479
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question