M
MattJ83
Hi,
Im currently inserting data into a database from a log file. This is
done by matching words from a log file and placing the line of the log
file into the database field. This is repeated for each log file in the
directory.
Currently I can get the information into the correct fields of the
database.
My problem is when a word is not matched in the log file that the
script is looking through and thus, not placed into the database field.
As a result, the next line of information is not then imported into the
log file when it should be! The script should not match the previous
word - leave the field in the database blank and then carry on with the
pattern matching - filling in the remaining fields.
Current code (not working code because i think you need to have a
database to understand the problem - and im not sure how placing
working code (without database) would help!:
#!/usr/central/bin/perl
use strict;
use warnings;
use DBI;
my @filenames = </home/username/logs/*.log>;
foreach my $filename (@filenames)
{
open my $LOG, '<', $filename or die "can't open $filename: $!\n";
my ($info, $elapsed1, $fast, $elapsed2, $inversions, $elapsed3 );
while(<$LOG>)
{
chomp;
{
if (/updates table/)
{
print "$_\n";
$info = $_;
}
if (/elapsed/)
{
print "[$_]\n";
if ($inversions)
{
$elapsed3 = $_;
}
elsif ($fast)
{
$elapsed2 = $_;
}
elsif ($info)
{
$elapsed1 = $_;
}
last;
}
}
if (/FASTSEARCH|conflicting/)
{
print "$_\n";
$fast = $_;
}
if (/inversions/)
{
print "$_\n";
{
$inversions = $_;
}
}
}
my $dbh = DBI ->connect("dbi:Oracle:server", "database", "password")
or die "couldn't connect to database: $DBI::errstr\n";
$dbh->do("INSERT INTO TEST2 (sfd, w, sdf, hfg, rt, bv, te)
VALUES (?, ?, ?, ?, ?, ?, ?)", undef,
$filename, $info, $elapsed1, $fast, $elapsed2, $inversions,
$elapsed3);
}
When i change 'elsif ($fast)' to ($fast or " ") it solves the problem
(includes the field after the blank field) but it leaves the whole
column of elapsed1 blank - is this because it is in a loop and can only
do 1 thing or another but not both? Is there a way to solve this?
I have also tried changing the last elsif statement to 'else' but to no
avail.
Thanks
Im currently inserting data into a database from a log file. This is
done by matching words from a log file and placing the line of the log
file into the database field. This is repeated for each log file in the
directory.
Currently I can get the information into the correct fields of the
database.
My problem is when a word is not matched in the log file that the
script is looking through and thus, not placed into the database field.
As a result, the next line of information is not then imported into the
log file when it should be! The script should not match the previous
word - leave the field in the database blank and then carry on with the
pattern matching - filling in the remaining fields.
Current code (not working code because i think you need to have a
database to understand the problem - and im not sure how placing
working code (without database) would help!:
#!/usr/central/bin/perl
use strict;
use warnings;
use DBI;
my @filenames = </home/username/logs/*.log>;
foreach my $filename (@filenames)
{
open my $LOG, '<', $filename or die "can't open $filename: $!\n";
my ($info, $elapsed1, $fast, $elapsed2, $inversions, $elapsed3 );
while(<$LOG>)
{
chomp;
{
if (/updates table/)
{
print "$_\n";
$info = $_;
}
if (/elapsed/)
{
print "[$_]\n";
if ($inversions)
{
$elapsed3 = $_;
}
elsif ($fast)
{
$elapsed2 = $_;
}
elsif ($info)
{
$elapsed1 = $_;
}
last;
}
}
if (/FASTSEARCH|conflicting/)
{
print "$_\n";
$fast = $_;
}
if (/inversions/)
{
print "$_\n";
{
$inversions = $_;
}
}
}
my $dbh = DBI ->connect("dbi:Oracle:server", "database", "password")
or die "couldn't connect to database: $DBI::errstr\n";
$dbh->do("INSERT INTO TEST2 (sfd, w, sdf, hfg, rt, bv, te)
VALUES (?, ?, ?, ?, ?, ?, ?)", undef,
$filename, $info, $elapsed1, $fast, $elapsed2, $inversions,
$elapsed3);
}
When i change 'elsif ($fast)' to ($fast or " ") it solves the problem
(includes the field after the blank field) but it leaves the whole
column of elapsed1 blank - is this because it is in a loop and can only
do 1 thing or another but not both? Is there a way to solve this?
I have also tried changing the last elsif statement to 'else' but to no
avail.
Thanks