Re: Grep on a list of files in directory
by Khen1950fx (Canon) on Oct 29, 2010 at 07:23 UTC
|
You almost had it. Try this:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper::Concise;
use File::Grep qw(fgrep);
my $start_dir = '/root/Desktop/server1/tools';
my $filename = 'load.c';
opendir(DIR, $start_dir)
or die "Could not open $start_dir\n";
my @xml_files = grep(/\.xml$/, readdir DIR);
closedir DIR;
print "XML FILES: ", "\n";
foreach my $xml_files(@xml_files) {
print $xml_files, "\n";
}
open(STDOUT, '>', $start_dir);
my @filematches = fgrep{ /^$filename$/ }
glob "/root/Desktop/server1/tools/*.xml";
print STDOUT $_ foreach Dumper(@filematches);
close STDOUT;
| [reply] [d/l] |
Re: Deferencing a hash
by graff (Chancellor) on Oct 29, 2010 at 03:49 UTC
|
You need to heed the point in the first reply about checking the return value from opendir, because the other reply (about the backslash problem) is probably causing opendir to fail.
But your description is confusing; in the code snippet you say "All the xml files print fine", but at the end you say "why isn't the code printing anything...?" Is it printing xml file names or not?
Also, why are you specifically looking for strings containing '/load.c#' (the regex in the 2nd grep call) in a list of file names that end with ".xml" extensions? Do you actually have xml files with '/' and '#' (and "load.c" in between) as part of the file names?
Updated to fix misspelling in the regex in the 3rd paragraph. Also, what is the relevance of the thread title? That is, how does anything in the OP relate to "Dereferencing a hash"? (I didn't see any hashes in the OP...) | [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Yes. Grep looks for the value in $_, which are filenames that end in .xml .
| [reply] |
Re: Deferencing a hash
by kcott (Archbishop) on Oct 29, 2010 at 02:58 UTC
|
Take a look at readdir. It doesn't return a list! My apologies - it does in list context. Take a look anyway for an example of the error checking.
You should add some error checking to opendir. Also, use strict; and use warnings; may provide useful information.
Update: As above - thanks graff
| [reply] [d/l] [select] |
|
|
Take a look at readdir. It doesn't return a list!
Um, look again.
readdir DIRHANDLE
Returns the next directory entry for a directory opened by
"opendir". If used in list context, returns all the rest of
the entries in the directory. If there are no more entries,
returns an undefined value in scalar context or a null list in
list context.
Using readdir in a list context is a frequently used idiom.
| [reply] |
|
|
| [reply] |
Re: Deferencing a hash
by UVB (Acolyte) on Oct 29, 2010 at 03:24 UTC
|
It looks like there is a bug in the first line. I think the backslash characters need to be escaped.
my $start_dir = "\\\\server1\\tools";
| [reply] |
|
|
| [reply] |
Re: Grep on a list of files in directory
by Dru (Hermit) on Oct 29, 2010 at 19:13 UTC
|
A simpler (IMHO) alternative to using grep here is to use glob
@xml_files = glob($start_dir . '\' . '*.xml');
YMMV
Thanks,
Dru
Perl, the Leatherman of Programming languages. - qazwart
| [reply] [d/l] |
|
|
syntax error at xml.pl line 32, near "*."
Bad name after xml' at xml.pl line 32.
| [reply] [d/l] |
|
|
@xml_files = glob($start_dir . '\\' . '*.xml');
| [reply] [d/l] |