Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > grep

Reply
 
 
chet
Guest
Posts: n/a
 
      05-12-2004
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;
}
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-12-2004
chet wrote:
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Eric Bohlman
Guest
Posts: n/a
 
      05-12-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (chet) wrote in
news:(E-Mail Removed) m:

> 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.

 
Reply With Quote
 
Web Surfer
Guest
Posts: n/a
 
      05-13-2004
[This followup was posted to comp.lang.perl.misc]

In article <(E-Mail Removed)> ,
(E-Mail Removed) says...
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix commands implemented ? (grep, wc, cat...) Spendius Java 2 12-13-2004 01:58 PM
Grep Hans Bijvoet Java 5 11-20-2004 01:52 AM
perl vs Unix grep Al Belden Perl 1 07-07-2004 05:58 AM
s/// has apparent side effect on grep() John E. Jardine Perl 2 04-13-2004 08:45 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM



Advertisments