NTGeek has asked for the wisdom of the Perl Monks concerning the following question:

Monks:

Okay, I have spent the past 45 minutes coding this entry in the hopes that it serves a lesson to others. Make sure that your question is properly formatted lest you receive an immediate reprimand.

This will be a clarification of my first post wherein I deleted my original entry.  Those who provided insight, however, have my most heartfelt thanks even though it resulted in errors because I was on a Windows Machine

To start, I am working on a Windows XP machine with C:/PERL being home to all of my data and scripts for the purposes of these exercises produces the following.

C:/PERL/dread.log  (a simple text file)

Within dread.log are the following entries:
This has no importance
This really has no importance
This is just plain boring
This is really important
This is blah blah blah
This is important to someone else
This is very important to you"
This is blah blah blah
The specific phrases (patterns?) that I want to spot are 'This is really important' as well as 'This is very important to you'

My goal is to create a script that I can execute against this log, or several logs, to look for specific phrases/patterns.

When these patterns are spotted, I would like them to be dumped to another file, say C:/PERL/warnings.txt

I think that the poor man's logic behind this would be.
a. open the dread.log file
b. search the dread.log file for specific phrases/patterns
c. write out a new log file called C:/PERL/warnings.txt

I know that on Linux it would be a matter of EGREP, however, since I am on a Windows machine I am going to have to accomplish this through PERL code alone.

I am not necessarily looking for the answer, as this would defeat the purpose of using this as a learning opportunity, however, at the moment I am definitely running in lost circles.

Regards,

Doc Wally

# Okay, here is what a part of my log file looks like :
#:
#=~= PuTTY log 2008.07.01 12:18:34 :
#login as: BOO:
#bobthePC@10.105.102.62's password::

#"This is really important"
#"This is very important to you"
#"This is also important"

#CRON: 2008/06/26 04:35:00 [0] Another backup/restore operation is already running.
#CRON: 2008/06/26 04:40:00 [0] Another backup/restore operation is already running.
#CRON: 2008/06/26 04:45:00 [0] Another backup/restore operation is already running.
#CRON: 2008/06/26 04:50:00 [0] Another backup/restore operation is already running.
#CRON: 2008/06/26 04:55:00 [0] Another backup/restore operation is already running.

#--further down the log file, I may have an entry that says

#CRON: 2008/06/30 05:30:00 1 Device error - I0302 (example)
# # #now here is a slight modification of the script that I got from the Waldner earlier

use strict; use warnings;
# okay, I get that part
open ERROR, "dread.log" or die "Can't open input file";
# open the log file, or die - a bit extreme?
open OUT, ">warnings.txt" or die "Can't open output file";
# put your results in a file called warnings.txt
while (<ERROR>)
{ print OUT if /^("This is really important"|"This is very important to you"|"This is also important")$/;
}
# Problem is, if the text in the original log file is not in quotes, so the warnings.txt file output is # simply

#"This is really important"
#"This is very important to you"
#"This is also important"
#
# I understand that this has to do with getting the regexes right - arggg, any breadcrumbs?

close ERROR;
close OUT;

AHA!!!

while (<ERROR>)
{ print OUT if (/This is really important/ | /restore operation is already running/ | /bobthePC/ | /This is also important/);

The phrases that are between the "/" will be spit out to the export file in the order that they appear in the log itself - so long as the actual data itself does not contain a "/" I should be fine. Of course, there is a way to contain this to permit for the "/" to be used, however, that is for tomorrow. Now I need a drink.
  • Comment on Reading a Log File - "pattern detection" Part 2

Replies are listed 'Best First'.
Re: Reading a Log File - "pattern detection" Part 2
by Corion (Patriarch) on Jul 01, 2008 at 14:24 UTC
Re: Reading a Log File - "pattern detection" Part 2
by waldner (Beadle) on Jul 01, 2008 at 14:51 UTC
    Well, something along the lines of:
    perl -ne 'print if /^("This is really important"|"This is very importa +nt to you"|"This is also important")$/' dread.log > warnings.txt
    Or, more verbosely:
    use strict; use warnings; open DREAD, "dread.log" or die "Can't open input file"; open OUT, ">warnings.txt" or die "Can't open output file"; while (<DREAD>) { print OUT if /^("This is really important"|"This is very important t +o you"|"This is also important")$/; } close DREAD; close OUT;
    The hardest part is getting the regexes right, to match only lines you're interested in. To be more accurate, you have to provide a realer (?) example.
Re: Reading a Log File - "pattern detection" Part 2
by linuxer (Curate) on Jul 01, 2008 at 19:32 UTC

    If you used code tags for your script and logfile examples, your post would've be almost perfect ;o))

    If your only intention is to read from a logfile, and the program can't open it for reading, why is it hard, when the program dies if it can't open that logfile?

    | (binary OR) is propably the wrong operator as you can reread in perldoc perlop. You are looking for the logical OR to combine your regex's.

    If you need to have '/' in your patterns you should mask it ( '\|' ) or change the delimiters of your regex ( m{text/with/slash} ). See perldoc for details.

    Perl Regex Tutorial
    Perl Regex Quickstart
    Perl Regex

Re: Reading a Log File - "pattern detection" Part 2
by Aim9b (Monk) on Jul 01, 2008 at 17:29 UTC
    I have a script that searches for a single pattern, then displays the next few lines. It's not a log file, but more of an ini file. I search for "section-name", etc. & then just prints that section, either n lines, or until xxx is found. If you'd like it, I can put it on my scratch pad. Good luck. I'm in a heavy learning curve myself. ;-)
      Essentially, if your log file contained the following:

      This is driving me nuts
      Its time for coffee
      Time to start drinking heavily

      . . . and you wanted your script to find one or more "patterns"

      then your

      print OUT if (/Its time fore coffee/ | /Time to start drinking heavily/);

      would need to be included. The "/" are what does the trick along with a decent text editor that will turn different colors when you get things right or wrong.

Re: Reading a Log File - "pattern detection" Part 2
by rovf (Priest) on Jul 02, 2008 at 08:11 UTC
    I know that on Linux it would be a matter of EGREP, however, since I am on a Windows machine I am going to have to accomplish this through PERL code alone.

    Sure you'll be more flexible by doing it in Perl, but let me point out that there is egrep for Windows. You can download it here: http://unxutils.sourceforge.net/. Other alternatives worth to be trying are ack or grepp.

    -- 
    Ronald Fischer <ynnor@mm.st>