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

Dear Monks,

This will be my first post - Indeed, I am working on my first attempts at PERL, so my knowledge is extremely rudimentary. I assure you that I have gnawed on a few pencils before posting this.

I have a log file ('dread-etc.log'). Inside of that log file are the following phrases. . .

"this message has no use whatsoever" "data, blah, blah, blah" "this message has no value, only confusion" "data, blah, blah, blah" "this is a bad error message" "data, blah, blah, blah"

My goal is to be able to run a perl script against this file so that it pulls out only the data (pattern?) that I have defined ("this is a bad error message"). Since this hypothetical file is filled with all types of nonsense, I want to be able to pull out only what I want and create another file. For now, even if the file is empty that would be a good start.

I think that the poor man's logic behind this would be.

a. open the dreadetc.log file b. search the dreadetc.log file for specific error messages ($x = "thi +s is a bad error message") c. write out a new log file called only_the_dread.txt

I thank you for your time in advance,

Doc Wally

Update:

Dear Monks,

Thanks for the replies. I am actually on a Windows machine.

Any assistance is greatly appreciated.

Regards and thanks,

Doc Wally

20080701 Janitored by Corion: Restored original content, added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Locating "Patterns" in a log file (e.g. keywords, etc)
by almut (Canon) on Jun 30, 2008 at 19:40 UTC
    I have a log file ('dread-etc.log'). (...) My goal is to be able to run a perl script against this file so that it pulls out only the data that I have defined ("this is a bad error message"). (...) and write out a new log file called only_the_dread.txt.

    There are many ways to do this... Here's a command line one-liner for the lazy:

    perl -ne "print if /this is a bad error message/" dread-etc.log >onl +y_the_dread.txt

    The part (pattern) in between the slashes is a regular expression, see perlre on how to craft them...

    Type "perl -h" for a short description of what the options/switches (i.e. -ne) do — the long version is found in perlrun.

    Also note that you might want to use single or double quotes, depending on which OS you're on (generally: single quotes on Unix, double quotes on Windows).

    ___

    (Update: quoted parts of the original question, as it has been deleted)

Re: Locating "Patterns" in a log file (e.g. keywords, etc)
by apl (Monsignor) on Jun 30, 2008 at 19:49 UTC
    To slightly generalize what others have told you, take a look at Re: Problem printing contents of file on how to open and loop through a specific file.

    You'd have to modify the code to use the regular expression almut showed you.

Re: Locating "Patterns" in a log file (e.g. keywords, etc)
by linuxer (Curate) on Jun 30, 2008 at 19:39 UTC

    If it's a Linux/Unix system you can use the system's tool grep:

    grep PATTERN FILE_TO_SEARCH_IN > FILE_WITH_SEARCH_RESULTS

    More info can be found in the manpage of grep.

    a simple perl solution could look like this (using some perl magic):

    #!/usr/bin/perl use strict; use warnings; my $pattern = shift @ARGV; while (<>) { print if m/$pattern/; }

    Save that to a file and execute it this way:

    perl script.pl PATTERN FILE_TO_SEARCH_IN > FILE_WITH_SEARCH_RESULTS

    update:
    formatting changes
    perl example added

Re: Locating "Patterns" in a log file (e.g. keywords, etc)
by linuxer (Curate) on Jun 30, 2008 at 21:26 UTC

    I wonder, where your original text has gone?

    If you need further assistance, you should have left it there, so others could read your description of your problem.

    If the given answers aren't sufficient, you should specify where you encounter your problems.