File::Grep

G

g4173c

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
 
M

Mumia W.

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);
 
X

Xicheng Jia

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
 
J

John W. Krahn

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
 
M

Mumia W.

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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top