grep

C

chet

the following script will parse records out of a log file.

my second grep doesn't find any records when it should
anybody know why (they exist in the file).

-thanx in advance.


#!/usr/bin/perl

#make te function calls
parse_lab_share ();

sub parse_lab_share ()
{
my @ins;
my @outs;
my @fields;
my $record;
my $login_str;
my $logout_str;
my $i;

unless ( open VNTILOG, "./vnti.log" )
{
die "Cannot open the vector log file: $!";
}
@ins = grep /VECGI/
&& !/ERROR/
&& !/Error/
&& !/Fragment/,
<VNTILOG>;
foreach ( @ins )
{
@fields = split;
if ( $fields[14] eq "in" )
{
$login_str =
"$fields[2]-$fields[1]-$fields[5]$fields[3]";
$record =
"$fields[23],$login_str,$fields[10],$fields[17]";
@outs = grep /$fields[23]/, <VNTILOG>;
foreach ( @outs )
{
@fields = split;
print ( "$fields[23]\n" );
}
}
}

#close the vector log file
close VNTILOG;
}
 
G

Gunnar Hjalmarsson

chet said:
the following script will parse records out of a log file.

my second grep doesn't find any records when it should

Should it? Which debug efforts have you carried out yourself?

Have you, for instance, considered the implication of the VNTILOG
filehandle being empty when you apply grep() to it?
 
E

Eric Bohlman

(e-mail address removed) (chet) wrote in
the following script will parse records out of a log file.

my second grep doesn't find any records when it should
anybody know why (they exist in the file).

-thanx in advance.


#!/usr/bin/perl

No "use warnings;".
No "use strict;".
#make te function calls

Completely useless comment.
parse_lab_share ();

sub parse_lab_share ()
{
my @ins;
my @outs;
my @fields;
my $record;
my $login_str;
my $logout_str;
my $i;

It's best to declare your variables with as narrow a scope as possible.
unless ( open VNTILOG, "./vnti.log" )
{
die "Cannot open the vector log file: $!";
}
@ins = grep /VECGI/
&& !/ERROR/
&& !/Error/
&& !/Fragment/,
<VNTILOG>;

Here you've read through the entire contents of your logfile, filling @ins
with the entries that match your criteria.
foreach ( @ins )
{
@fields = split;
if ( $fields[14] eq "in" )
{
$login_str =
"$fields[2]-$fields[1]-$fields[5]$fields[3]";
$record =
"$fields[23],$login_str,$fields[10],$fields[17]";
@outs = grep /$fields[23]/, <VNTILOG>;

And now you're trying to read through what's left of your log file after
you've already read through all of it. Hmmm. Even worse, you're trying to
do it for every single matching record.
foreach ( @outs )
{
@fields = split;
print ( "$fields[23]\n" );
}
}
}

#close the vector log file

YAUC (Yet Another Useless Comment).
close VNTILOG;
}

Nowhere do you explain *what* you're trying to accomplish. I can only
guess that you're trying to do something resembling a relational join of
the logfile on itself, but it would really help to know why.
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

the following script will parse records out of a log file.

my second grep doesn't find any records when it should
anybody know why (they exist in the file).

-thanx in advance.


#!/usr/bin/perl

#make te function calls
parse_lab_share ();

sub parse_lab_share ()
{
my @ins;
my @outs;
my @fields;
my $record;
my $login_str;
my $logout_str;
my $i;

unless ( open VNTILOG, "./vnti.log" )
{
die "Cannot open the vector log file: $!";
}
@ins = grep /VECGI/
&& !/ERROR/
&& !/Error/
&& !/Fragment/,
<VNTILOG>;
foreach ( @ins )
{
@fields = split;
if ( $fields[14] eq "in" )
{
$login_str =
"$fields[2]-$fields[1]-$fields[5]$fields[3]";
$record =
"$fields[23],$login_str,$fields[10],$fields[17]";
@outs = grep /$fields[23]/, <VNTILOG>;
foreach ( @outs )
{
@fields = split;
print ( "$fields[23]\n" );
}
}
}

Your 1st grep statement reads in *ALL* the records from the open file,
so the 2nd grep statement has nothing left to read.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top