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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: regular expression help
by Velaki (Chaplain) on Aug 14, 2007 at 18:17 UTC

    Here's a contrived example

    #!/usr/bin/perl # Never leave home without these! use strict; use warnings; # Loop through the faux data log, and... while (<DATA>) { # ...find lines that might be interesting... if (/--/) { # ... but ignore certain ones, like those # that say "logging" or "disconnected". unless ( /logging/ || /disconnected/ ) { # Print the rest. print "NOTE: $_"; } } } # Our faux data for this example __DATA__ 081407 start up server 081407 -- fatal error in procedure 1 081407 -- disconnected user 081407 -- logging started 081407 -- not enough space

    This produces:

    NOTE: 081407 -- fatal error in procedure 1 NOTE: 081407 -- not enough space

    Hope this helps,
    -v

    "Perl. There is no substitute."

      I like Velaki's approach; take everything and filter out the ones you know you want to ignore. If something new comes in, you can assess that and then update your ignore list if it's not important.

      If you did it the other way (listing only what you want), then something new might be ignored that you actually want.

      In the next value frame, you might externalize the list of known "ignore" patterns. That way you wouldn't be updating your code, but a config file instead.


      I humbly seek wisdom.
      thank you guys for helping a noob. you have helped a lot! thanks velaki for giving me that code!
        If you want to look up the errors in the logfile for context later on, it would be handy to also print out $. which will contain the line number of the logfile you are currently reading.
Re: regular expression help
by FunkyMonk (Bishop) on Aug 14, 2007 at 17:42 UTC
    It's difficult to help you when you've made it so difficult to understand what you want because of the poor formatting of your post. I've made my best shot, is this the sort of thing you want?
    while ( <DATA> ) { next if m/(?:start up server)|(?:-- logging started)|(?:-- disconn +ected user)/; print } __DATA__ 081407 start up server 081407 -- fatal error in procedure 1 081407 -- disconnected user 081407 -- logging started 081407 -- not enough space

    Output:

    081407 -- fatal error in procedure 1 081407 -- not enough space

    You should ask your friend to tell you how to format your posts, and read How do I compose an effective node title? and Writeup Formatting Tips.

      sorry for the bad formatting. its my first time to use this. i was thinking more of somethingl ike this:
      $regex = qr/--/ if($thestring =~ $regex || $thestring !~ $regex) { unshift(@match, $thestring); }
      but the problem is the logfile is a bit dynamic and i dont know what errors i am going to get. i just know that "--" usually indicates an error but not all lines with "--" means that they are really errors. so in this sample log file:
      081407 start up server 081407 -- fatal error in procedure 1 081407 -- disconnected user 081407 -- logging started 081407 -- not enough space
      i just want to get
      081407 -- fatal error in procedure 1 081407 -- not enough space
      and disregard the rest like:
      081407 -- disconnected user 081407 -- logging started
      is there a way i can use it in qr// function? i guess something like this:
      1. get all occurrences of "--". 2. in those occurrences of "--" ignore those that have "logging" and " +disconnected" could it be place in a qr// function like: qr/-- || ~! logging,disconnected/ (wrong syntax of course) something like an ignore list.
      hope thats clearer
        but the problem is the logfile is a bit dynamic and i dont know what errors i am going to get. i just know that "--" usually indicates an error but not all lines with "--" means that they are really errors. so in this sample log file
        Well then, your first step is to find out either what you want, or what you don't want. No regular expression is going to solve that one for you.

        Thanks for taking the time to properly format your post. It is most appreciated.

Re: regular expression help
by toolic (Bishop) on Aug 14, 2007 at 17:02 UTC
    First suggestion: add code tags so that we can distinguish your question(s) from your input file contents. See Writeup Formatting Tips.

    Second suggestion: provide some sample expected output.

    It will be much easier for others to help you if you follow these simple suggestions.

Re: regular expression help
by SuicideJunkie (Vicar) on Aug 14, 2007 at 17:49 UTC

    The post box doesn't automatically convert \n to <br>

    You've gotta put in your own html. <p></p> blocks, and an unordered list <ul></ul> would work well for your post.

    Definitely strange if you're used to message boards with WYSIWYG posting.