?
Solved

error.log displaying and reg exp

Posted on 2003-03-29
19
Medium Priority
?
207 Views
Last Modified: 2010-03-05
i have decided to make a perl program that takes the contents of the file error.log and splits it into a date, a type and a error. the format of the file is

[date][type]message/n

i have split it into lines but now how do i split it into date,type and message. do i use rgular expressions.

cheers for your time
0
Comment
Question by:stiletto2003
[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
19 Comments
 
LVL 1

Expert Comment

by:Trevize
ID: 8231629
Yes, the easiest way to do this is to use non-greedy regular expressions:

my $str = "[date][type]error\n";

my ($date, $type, $error) = $str =~ /^(\[.*?\])(\[.*?\])(.*)/;
0
 

Author Comment

by:stiletto2003
ID: 8231650
wait there
0
 

Author Comment

by:stiletto2003
ID: 8231655
so i would just enter that code right?
and $date would be the date and so on\
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:stiletto2003
ID: 8231731
what i have is a foreach loop and inside it makes a string for the current line. in the foreach loop i have also put
my ($date, $type, $error) = $theerror =~ /^(\[.*?\])(\[.*?\])(.*)/;
theerror being the line
i then print
print "$date,,$type,,$error";
and i get ,,,, as the output
do you understand what i have done if not say so and i will explain further
cheers
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8231951
Generally, you code would look something like:

#!/usr/bin/perl

open LOG, "error.log" or die "Can not open error.log $!\n";

while (<LOG>) {
  my ($date, $type, $error) =~ /^(\[.*?\])(\[.*?\])(.*)/;
  print "$date,,$type,,$error";
}

close LOG;
0
 

Author Comment

by:stiletto2003
ID: 8231984
oops i cant decrease then i will give u all and hopefully u can help me again
0
 

Author Comment

by:stiletto2003
ID: 8231985
oops i cant decrease then i will give u all and hopefully u can help me again
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 8232602
Do you want to keep the [] brackets that are around date and type in the vars?  If not then try this regx:

/^\[(.*?)\]\[(.*?)\](.*)/;

I haven't tested it but I think it should work.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 8232609
Or this:

/^\[(.+)\]\[(.+)\](.+)/;
0
 

Author Comment

by:stiletto2003
ID: 8233310
guys the obvious thing which i found after a bit of research was
/\[(.+?)\]/g;
and then the acuatl error msg being $' this seems to work really well but now i have another samll problemo. i may be able to give points for this one. it is on the same topic so i dont think i should do another thread.

#!C:\Perl\bin\perl.exe
# open the file for reading
open(DAT,"c:/program files/apache group/apache2/logs/error.log") || die("Cannot Open File");
@thearray = <DAT>;
close(DAT);

foreach $anything (@thearray)
{
$' =~ s/C/h/g;

print "$'";
}

its the $' =~ s/C/h/g that doesnt work. what i am trying to do is take all the html out of the error message so i would do something like

$' =~ s/[<>]//g

this shoould take out all the < or > therefore stopping all html code. i have replaced the line
$' =~ s/C/h/g;
with the code above but the output is blank it is also blank with the first code bit

cheers
0
 

Author Comment

by:stiletto2003
ID: 8233759
any ideas?










0
 

Author Comment

by:stiletto2003
ID: 8234925
do u guys mind if i dont give any1 the points cause i dont really know who to give them to
i am inclined towards trevize but fishmonger and titin tried just as much
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 8236070
Since I’m still at my brothers house, I’m unable to test some possible solutions to your second question, but if you wish to close this out, I’d say you should award the points the person that was most helpful to you.  If you want to keep it open to find an answer to the other related question, I would not object however, generally speaking, it should be a submitted as a separate question.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 225 total points
ID: 8236149
One additional note that might help when you make your decision to award the points or not.  According to your original post, we thought you wanted a regex that would match and save each field. Had we known that you only wanted to separate out the error message, we would have come up with the proper expression.

If you can include your code and some lines from the error message we might be able to find a better solution.
0
 

Author Comment

by:stiletto2003
ID: 8237010
thanks fishmonger that was very helpful. since you think tou need my code i will give it to you
##########################################################
#!C:\Perl\bin\perl.exe
# open the file for reading
open(DAT,"c:/program files/apache group/apache2/logs/error.log") || die("Cannot Open File");
@thearray = <DAT>;
close(DAT);

# set it up for internet pages i.e: html
print "Content-type: text/html\n\n";
print "<HTML><BODY>";
#print "<table border = \"1\">";
#next bit
foreach $anything (@thearray)
{
#this next line was for debug and just prints the error #msg line by line
#print "$anything<br>";
#this is the first regexp
($date, $type) = $anything =~ /\[(.+?)\]/g;
#and here is the one that doesnt work
$' =~ s/C/h/g;
print "$'";
#print "<tr><td>$date</td><td>$type<hr size=\"1\">$'";
}


######################################################
you may not get some of the comments but the rest of the code should make sense.
thanks in advance
0
 

Author Comment

by:stiletto2003
ID: 8237021
i have also decided to give fishmonger the points because he posted the most and came up with very reasonable awnsers for what he thought i was doing. sorry if you think that you should have got the points
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 8240139
$' =~ s/C/h/g;
Failed because you're trying to modify a read only variable.

You need to create another var to hold the error message.  This can be done in the same line that you assigned the $date and $type.

Here is the code that I tested on my system.

*********************

#!c:\program files\perl\bin\perl.exe -w

use strict;
my (@errors, $date, $type, $error);

open (LOG, "c:/program files/apache2/logs/error.log") or die "cannot open log file <$!>";
@errors = <LOG>;
close (LOG);

foreach (@errors) {
    ($date, $type, $error) = $_ =~ /\[(.+)\] \[(.+)\] (.+)/;
    $error =~ s/[<>]//g;     # depending on exactly what you're trying to accomplish,
               # this regx could be changed to s/\b<.+>\b//g
}

*********************

Take note that I have turned on warnings (only while writing/debugging) and I'm using the strict pragma.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 8240283
I appreciate that you awarded me the points but why the grade B?  The regx that you chose to use is a variation of the one that the rest of us suggested.  Yours doesn't fully accomplish what you want, however ours does.  If you had waited a little longer (10 more hours, as it turns out),  you would have received a complete and correct answer.  If you feel so inclined, you can ask to have the grade changed to an A.

One additional suggestion on developing your code.  Download Komondo http://www.activestate.com/Products/Komodo/, it's an IDE GUI front end to perl (and several other scripting languages) that will help you in you're writing and debugging of your scripts.  Here's another link that has several other Perl IDEs. http://www.cpan.org/ports/index.html#win32
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 8240418
One final comment on your code.  You're wanting to setup your output as an HTML file but, your code is sending the print statements to <STDOUT>.  You need to open an output filehandle to a HTML file and sent the print statements to that filehandle.  You may also want to look at using some of these modules. http://www.cpan.org/modules/by-category/15_World_Wide_Web_HTML_HTTP_CGI/
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 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