Hi,

I have a requirement to get all those file names from a directory whose contents mataches with a specific pattern letsay pattern 'ABCDE'
Can you please provide me the code to meet this requirement.

Thanks

Hi realoneomer,
It's good to always give a trial on something, even when someone is new to such and then ask for help, expect you are really to pay for the services you are asking for.
But let me assume you are totally new on perl. I would give you a head up and I believe you should jump start with that.

There are more than one way to do anything in Perl. I will show you 2 at least.

  1. You achieve what you wanted, you can, open a directory, then step through the files one at a time using such as loop, then using the regular expression, you can find the files that match. Example:

    #!/usr/bin/perl -w
    use strict;
    
    my $dir = $ARGV[0];    # get the directory from CLI
    
    $dir ||= '.';          # if no directory is given us the current one
    
    my $pattern = qr/\.pl$/;    # search pattern
    
    opendir my $dh, $dir or die "can't open directory: $!";
    
    while ( readdir($dh) ) {
        print $_, $/ if /$pattern/i;
    }
    
    closedir $dh or die "can't close dir: $!";
    
  2. The code above works well, yet we ask why reinvent a wheel, if you can't do a better job than the last one?
    So, instead of doing all the job, why not use modules that are clipped with your installed perl? Moreso, you have to search directories that are within your primary directory, it becomes a different ball game altogehter.
    You can simple do all that and more using the module Find::File like so:

      #!/usr/bin/perl -w
      use strict;
      use File::Find qw(find);
    
      find(
         sub {
    
            print $_, $/ if /\.pl$/i;
         },
         '.'
      );
    

It does all the jobs and even more with little codes to even write.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.