Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Grep on a list of files in directory

by iphone (Beadle)
on Oct 29, 2010 at 02:46 UTC ( #868208=perlquestion: print w/ replies, xml ) Need Help??
iphone has asked for the wisdom of the Perl Monks concerning the following question:

my $start_dir= "\\server1\tools"; my $file_name = "load.c"; my @filematches; opendir(DIR, "$start_dir"); @xml_files = grep(/\.xml$/,readdir(DIR)); print "XML FILES\n"; print "@xml_files\n"; --->All the xml files print fine closedir(DIR); push @filematches, grep( /\/\Q$file_name\E#/i, @xml_files); print @filematches;

why isn't the above code printing anything even though "load.c" matched the xml files in @xml_files?Pls help

Thanks

Comment on Grep on a list of files in directory
Download Code
Re: Deferencing a hash
by kcott (Abbot) 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

    -- Ken

      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.

        Thanks for pointing that out - I've updated the node.

        -- Ken

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";
      even better would be forward slashes, which don't need to be escaped, and perl takes care of the translation on unix/windows/other.
      the hardest line to type correctly is: stty erase ^H
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...)

      1.Fixed the title 2.xml files print fine at the line mentioned but the grep fails to print anything. i have an xml file with /load.c# in it,am expecting the grep to print it. Is it not printing because we are acutally grepping for the name of the xml files and not the content in it.????

        Yes. Grep looks for the value in $_, which are filenames that end in .xml .
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;
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

      If i use as suggested i am getting the following syntax error.Can you pls help?

      syntax error at xml.pl line 32, near "*." Bad name after xml' at xml.pl line 32.
        Escape the backslash:
        @xml_files = glob($start_dir . '\\' . '*.xml');

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://868208]
Approved by aquarium
Front-paged by aquarium
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-11-21 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (104 votes), past polls