Another Regular expression question

D

Dave Roberts

I wrote a program to sort my MySQL log files. It works fine, but I
get about a hundred errors such as:

Malformed UTF-8 character (unexpected continuation byte 0x88, with no
preceding start byte) in pattern match (m//) at ./subtest3.pl line 27,
<DB_Dump> line 1685.

I get an identical error for each of the two lines where I am doing
the regex matching. The part that puzzles me is that after converting
the files to hex, neither the source nor the destination files contain
any of the characters mentioned in the numerous errors.

Here's the code...

#! /usr/bin/perl -w
use strict;
my (@DB_List, @DB_Dump, $line, $DB, $DB_Start, $DB_End,
$Save_To_File);
unless (open(DB_Dump, "/test/log")) {
print ("File Problem");
}
unless (open(outfile, ">>/test/Sorted_DB_Dump")) {
print ("File Problem");
}
@DB_Dump=<DB_Dump>; #put file into a list

my @DB_List=("use sonia_db;","use maris_db;","use psa;"); # names of
three databases

foreach $DB (@DB_List){
print outfile ("$DB\n"); # Stores "use DB" command to file

foreach $line (@DB_Dump){

if ($line =~ m/^($DB)/){ # Check if current DB name is found
$DB_Start=1;
$Save_To_File=1; # save data to file
}
else {
$DB_Start=0;
}

if ($line =~ m/^use/){ # Check if new (unwanted) DB name is
found
$DB_End=1;
}
else{
$DB_End=0;
}

if ($DB_End==1 && $DB_Start==0){ # if the "use" is found and I
know it isn't the current DB
$Save_To_File=0; # stop writing data to file
}

if ($Save_To_File && !$DB_Start && !$DB_End){
print outfile ($line);
}
}
}
______________End of Program

Any ideas?

Thanks, Dave
 
P

Paul Lalli

More than likely, the lines of the file you're opening contain 'special'
characters. Perl interpolates the variable before passing it to the
RegExp engine. You need to escape any of the misbehaving characters:

if ($line =~ /^(\Q$DB\E)/)) { ... }

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top