Click to See Complete Forum and Search --> : Need some help with perl regex


quasimojo
12-28-2000, 08:51 AM
Hi, I have recently begun delving into the world of Perl programming. The goal of my first project is to create a perl script which will parse a unix hosts syslog.log file weed out "uninteresting" alerts to a new file and then remove those same uninteresting lines from the original syslog.log file.

The parse portion of the script has gone fine thus far. I have succeeeded in gathering and moving the unwanted portion using the following script:

$a = "/home/name/syslog.log"
$b = "/home/psmith/ocd.log"

open (IN,"$a") | | die "Cannot open $a for reading: $!";
open (OUT,">>$b") | | die "Cannot open $b for writing: $!"
while (<IN> ) {
if (/ocd/) {
print OUT "$_"
}
}

Here is the problem, how do I craft a regular expression to go back and remove any line containing the character string "ocd" from the original syslog file? I have successfully used a system call to grep:
system ("grep -v 'ocd' $a >> $b")

but this seems like a big cheat to me. Could someone please point me in the right direction?

Thanx

TheLinuxDuck
12-28-2000, 10:44 AM
One way to do it is to do similar to what you just did (dumping all lines that contain ocd to a new file) but dump the other lines to a different file.

Something like:


#!/usr/bin/perl -w
use strict;
use warnings;

$a = "syslog.log";
$b = "ocd.log";

open (IN,"$a") or die "Cannot open $a for reading: $!\n";
open (OUT,">>$b") or die "Cannot open $b for writing: $!\n";
open (ALTOUT,">>$a.copy") or die "Cannot open $a.copy for writing: $!\n";
while (<IN> ) {
if (/ocd/) {
print OUT;
}
else {
print ALTOUT;
}
}


------------------
TheLinuxDuck
I have a belly button.
:wq

jemfinch
12-28-2000, 12:37 PM
perldoc -f grep

I'd give you an example, but I don't have a computer to test it on.

Jeremy

quasimojo
12-28-2000, 01:05 PM
Thanks for the tips!! I'll give 'em a try and let you guys know... I really appreciate the help!

quasimojo
12-28-2000, 02:18 PM
Thanks guys worked like a charm!!! One question though, when I used the use strict; and use warnings; statements I got compilation errors back. When I took them out everything worked fine. What is the nature of those commands?

jemfinch
12-28-2000, 03:09 PM
"use strict" makes several bad practices in perl actually cause compilation errors rather than simply "doing the (hopefully) right thing", as perl would normally do.

Such bad practices include using undeclared variables, symbolic references, and some other stuff I don't remember.

Warnings are just warnings.

Jeremy

TheLinuxDuck
12-28-2000, 06:36 PM
As jemfinch said, use strict, in conjuction with perl -w are great programming practices.. they are basicly as an English teacher is for the english language.

use warnings just gives more verbose warning messages when there's a problem in the code.

The warnings are caused from:

$a = "/home/name/syslog.log"
$b = "/home/psmith/ocd.log"


where the variables are being created and given a vlue without being declared first.. if you add my to the front of each, it will remove the warnings..

If you declare multiple variables on the same line, they must be surrounded by parenthesis ().


my $a = "/home/name/syslog.log"
my $b = "/home/psmith/ocd.log"
#
# or
#
my ($a,$b);
$a = "/home/name/syslog.log"
$b = "/home/psmith/ocd.log"


It is a very good practice to use perl -w, and use strict. I don't hardly use use warnings, but was trying it out a little as I did the example for you. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

jemfinch
12-28-2000, 09:17 PM
Also note that lexical variables (ie, those declared by "my") are accessed faster than other kinds of variables.

Jeremy

quasimojo
12-29-2000, 08:44 AM
Thanks dude's! Remember that this was my first perl script. All I've ever done prior to this is shell scripting and VERY basic sed and awk. I tend to fall back on shell script type variable declarations.

I will use the my () function in this script. My whole reason for trying perl was for increased speed ( I only have 5 minutes to parse and rename an insanely large syslog.log file before the syslogd tries to write info from the kernel into it again).

I know it would be easier to simply redirect the out put from the deamon itself but my manager doesn't feel to comfortable with me messing around with HP standards. Oh well!

Again thank you for the help and I will probably be hitting this BBS for help again in the near future.

Pat Smith

jemfinch
12-29-2000, 06:04 PM
Originally posted by quasimojo:
My whole reason for trying perl was for increased speed ( I only have 5 minutes to parse and rename an insanely large syslog.log file before the syslogd tries to write info from the kernel into it again).


You may want to try a method of storing (persistently) your location in the logfile, and only parsing those parts which you haven't parsed yet.

I don't know the perl API for this, but in C, it would involve using fseek() and storing the off_t that was returned. I'm sure perl has some method of doing this.

Jeremy