grep

Discussion in 'Perl Misc' started by chet, May 12, 2004.

  1. chet

    chet Guest

    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;
    }
     
    chet, May 12, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, May 12, 2004
    #2
    1. Advertising

  3. chet

    Eric Bohlman Guest

    (chet) wrote in
    news::

    > 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.
     
    Eric Bohlman, May 12, 2004
    #3
  4. chet

    Web Surfer Guest

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

    In article <>,
    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.
     
    Web Surfer, May 13, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,494
    danpres2k
    Aug 25, 2003
  2. John E. Jardine

    s/// has apparent side effect on grep()

    John E. Jardine, Apr 12, 2004, in forum: Perl
    Replies:
    2
    Views:
    464
    John Jardine
    Apr 13, 2004
  3. Al Belden

    perl vs Unix grep

    Al Belden, Jul 3, 2004, in forum: Perl
    Replies:
    1
    Views:
    5,226
    Giridhar Nandigam
    Jul 7, 2004
  4. Hans Bijvoet

    Grep

    Hans Bijvoet, Nov 19, 2004, in forum: Java
    Replies:
    5
    Views:
    841
    David Zimmerman
    Nov 20, 2004
  5. Spendius
    Replies:
    2
    Views:
    3,026
    Rogan Dawes
    Dec 13, 2004
Loading...

Share This Page