error.log displaying and reg exp

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
stiletto2003Asked:
Who is Participating?
 
FishMongerConnect With a Mentor Commented:
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
 
TrevizeCommented:
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
 
stiletto2003Author Commented:
wait there
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
stiletto2003Author Commented:
so i would just enter that code right?
and $date would be the date and so on\
0
 
stiletto2003Author Commented:
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
 
TintinCommented:
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
 
stiletto2003Author Commented:
oops i cant decrease then i will give u all and hopefully u can help me again
0
 
stiletto2003Author Commented:
oops i cant decrease then i will give u all and hopefully u can help me again
0
 
FishMongerCommented:
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
 
FishMongerCommented:
Or this:

/^\[(.+)\]\[(.+)\](.+)/;
0
 
stiletto2003Author Commented:
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
 
stiletto2003Author Commented:
any ideas?










0
 
stiletto2003Author Commented:
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
 
FishMongerCommented:
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
 
stiletto2003Author Commented:
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
 
stiletto2003Author Commented:
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
 
FishMongerCommented:
$' =~ 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
 
FishMongerCommented:
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
 
FishMongerCommented:
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
All Courses

From novice to tech pro — start learning today.