File::Grep

Discussion in 'Perl Misc' started by g4173c@motorola.com, May 3, 2007.

  1. Guest

    Hi:

    I'm trying to get the match string from fgrep with the following code:

    use File::Grep qw (fgrep);
    foreach $log (@logfile) {
    @match = fgrep { /HOSTNAME/ } "$log";
    ...
    do_lots_more
    }


    however this is only returning a HASH(xxx). I assume it doesn't like
    the
    "$log" which is the path to the file. How can I correct this?

    Thanks!
    Tom
    , May 3, 2007
    #1
    1. Advertising

  2. Mumia W. Guest

    On 05/03/2007 10:36 AM, wrote:
    > Hi:
    >
    > I'm trying to get the match string from fgrep with the following code:
    >
    > use File::Grep qw (fgrep);
    > foreach $log (@logfile) {
    > @match = fgrep { /HOSTNAME/ } "$log";
    > ...
    > do_lots_more
    > }
    >
    >
    > however this is only returning a HASH(xxx). I assume it doesn't like
    > the
    > "$log" which is the path to the file. How can I correct this?
    >
    > Thanks!
    > Tom
    >


    No, it looks like a bug in the module. You might e-mail the author. This
    is a workaround for right now:

    my @matching_lines;
    fgrep { push @matching_lines, $_ if /HOSTNAME/ } $log;

    And here is another solution:

    use File::Slurp qw(read_file);
    my @matching_lines = grep /HOSTNAME/, read_file($log);
    Mumia W., May 3, 2007
    #2
    1. Advertising

  3. Xicheng Jia Guest

    On May 3, 11:36 am, wrote:
    > Hi:
    >
    > I'm trying to get the match string from fgrep with the following code:
    >
    > use File::Grep qw (fgrep);
    > foreach $log (@logfile) {
    > @match = fgrep { /HOSTNAME/ } "$log";


    1) you dont have to add quotation marks with $log.

    2) the returned array elements are designed as filehandle-like
    objects, not the filenames. If you print out the returned hash
    reference, you can easily know how to extract the corresponding
    filename..try the following:

    foreach my $log (@logfile) {
    my @match = fgrep { /HOSTNAME/ } $log;
    print $_->{filename} for @match;
    # do more stuff
    }

    Good luck,
    Xicheng
    Xicheng Jia, May 3, 2007
    #3
  4. wrote:
    >
    > I'm trying to get the match string from fgrep with the following code:
    >
    > use File::Grep qw (fgrep);
    > foreach $log (@logfile) {
    > @match = fgrep { /HOSTNAME/ } "$log";
    > ...
    > do_lots_more
    > }
    >
    >
    > however this is only returning a HASH(xxx). I assume it doesn't like
    > the "$log" which is the path to the file. How can I correct this?


    @ARGV = @logfile;

    my @match;
    while ( <> ) {
    push @match, $_ if /HOSTNAME/;
    }


    Or if you want @match to contain log file names:

    @ARGV = @logfile;

    my @match;
    while ( <> ) {
    if ( /HOSTNAME/ ) {
    push @match, $ARGV;
    close ARGV;
    }
    }





    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, May 3, 2007
    #4
  5. Mumia W. Guest

    On 05/03/2007 12:40 PM, Xicheng Jia wrote:
    > On May 3, 11:36 am, wrote:
    >> Hi:
    >>
    >> I'm trying to get the match string from fgrep with the following code:
    >>
    >> use File::Grep qw (fgrep);
    >> foreach $log (@logfile) {
    >> @match = fgrep { /HOSTNAME/ } "$log";

    >
    > 1) you dont have to add quotation marks with $log.
    >
    > 2) the returned array elements are designed as filehandle-like
    > objects, not the filenames. If you print out the returned hash
    > reference, you can easily know how to extract the corresponding
    > filename..try the following:
    >
    > foreach my $log (@logfile) {
    > my @match = fgrep { /HOSTNAME/ } $log;
    > print $_->{filename} for @match;
    > # do more stuff
    > }
    >
    > Good luck,
    > Xicheng
    >
    >


    Once again, it's proved how valuable Data::Dumper is. I still think
    File::Grep has a bug: it's documentation needs to specify that a list of
    hash references is returned.
    Mumia W., May 4, 2007
    #5
    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,449
    danpres2k
    Aug 25, 2003
  2. Abhijeet Dharmapurikar

    file grep

    Abhijeet Dharmapurikar, Aug 16, 2007, in forum: Ruby
    Replies:
    6
    Views:
    100
    Alexander Mcconaughey
    May 18, 2009
  3. qanda
    Replies:
    9
    Views:
    152
    qanda
    Sep 11, 2003
  4. qanda
    Replies:
    1
    Views:
    103
    Tassilo v. Parseval
    Sep 15, 2003
  5. J M
    Replies:
    2
    Views:
    143
    Darren Dunham
    Jan 26, 2004
Loading...

Share This Page