in reply to Re^2: Reading Directory and getting pattern from a file
in thread Reading Directory and getting pattern from a file

What do you have so far? Look at the examples provided with File::Find, and you can find others via Google and on this site.

By the way, "Dum Spiro Spero" is not my name, just a tagline. It means "while I breathe, I hope" in Latin.

Dum Spiro Spero
  • Comment on Re^3: Reading Directory and getting pattern from a file

Replies are listed 'Best First'.
Re^4: Reading Directory and getting pattern from a file
by Eshan_k (Acolyte) on Feb 10, 2015 at 17:54 UTC
    Sorry I was not aware its not your name and yes tag line is cool. So far I have this much. code, I want to go into subdirectories from there. and "mdp" is a pattern I want to match. Power artist has directories and in those there are several sub directories I just want to traverse and get the line from the file in those sub-directories. ---------------------------------------- 1 #!usr/bin/perl; 2 3 use strict; 4 use warnings; 5 6 7 $| = 1; 8 9 10 sub main{ 11 12 my $directory = '/nfs/fm/stod/stod1031/w.eakanoje.101/power_artist_2/'; 13 opendir (DIR, $directory) or die "Cannot open directory $directory"; 14 # while (my $file = readdir(DIR)){ 15 # next if($file =~ m/^\./); 16 # print "$file\n"; 17 # } 18 19 my @out = glob("mdp*"); 20 21 print "@out\n";

      Please put <c> ...</c> tags around your code. It is unreadable.

      Dum Spiro Spero
Re^4: Reading Directory and getting pattern from a file
by Eshan_k (Acolyte) on Feb 10, 2015 at 18:21 UTC

    Sorry, I was not aware of your name. Your tag line is cool. :) So far I have written this much of code. The power directory has two directories and in those directories. There are 130 sub directories for each directories. I want to go to each sub directory and get a pattern from a text file in that directory. I am stuck with how to check directories and get that perticular pattern. mdp is a pattern. Your help will be greatly appreciated.

    #!usr/bin/perl; use strict; use warnings; $| = 1; sub main{ my $directory = '/nfs/fm/stod/stod1031/w.eakanoje.101/power/'; opendir (DIR, $directory) or die "Cannot open directory $directory" +; # while (my $file = readdir(DIR)){ # next if($file =~ m/^\./); # print "$file\n"; # } my @out = glob("mdp*"); print "@out\n"; } main();
    ------------------------------------------

      Note: Do not include line numbers in your code posts. It makes them not compile.

      Go to your Profile->{settings}->{display settings}->{code Prefix} and set it to something like "&001;: "; That will give you line numbers when viewing on perlmonks without breaking the actual code.

      by
      get a pattern from a text file
      do you mean
      • list only file names that match a pattern
      • read a pattern from a file's contents?
      "get a pattern" sounds like the latter, but your example looks more like the former.

      I like Path::Tiny for such tasks, e.g. the following would show you all files in the tree where the name starts with "mdp". Note this doesn't use shell patterns like glob (with * and ?) but perl's own regexes.

      use strict; use warnings; use Path::Tiny qw/path/; for ( path('/nfs/fm/stod/stod1031/w.eakanoje.101/power/')->children( q +r/^mdp/ ) ) { print $_ . $/; }
        Thanks a lot soonix.