Pattern matching in Perl issue

Hello all,

I have written a perl script which searched for a file in a folder, and return the value found or not found.

The file that we are getting is a weekly file and everyweek, the date changes.

So for this i have written a file pattern matching script, but i am not getting the result.

The file is present in the folder mentioned, but it returns the result as "File not found".

Is the file pattern matching correct that i have used?

$filepattern = '.*A0PD7193.*DMECBID.SUPL.*\.txt';

The file name is

DMEP.A0PD7193.DMECBID.SUPL.2011042981697.txt

Please try with double quotes

$filepattern = ".*A0PD7193.*DMECBID.SUPL.*\.txt";

The example as given (even without the double quotes, which change nothing in this case) works for me in this simple way:

#!/usr/bin/perl -w

use strict;
use warnings;

my $filepattern = '.*A0PD7193.*DMECBID.SUPL.*\.txt';
my $filename    = 'DMEP.A0PD7193.DMECBID.SUPL.2011042981697.txt';

if ( $filename =~ /$filepattern/ ) {
    print "OK\n";
}
else {
    print "NOK\n";
}

Please post the relevant part of your script so that we can see if there maybe is a problem with the way you do the matching.

This pattern matching did work

my $filepattern = '.*A0PD7193.*DMECBID.SUPL.*\.txt';

The requirement is we get weekly files that will get loaded.
e.g.

DMEP.A0PD7193.DMECBID.SUPL.2011042981697.txt
DMEP.A0PD7193.DMECBID.SUPL.2011050681697.txt
DMEP.A0PD7193.DMECBID.SUPL.2011051381697.txt

and so on.

I cant keep changing the file name here everytime

my $filename    = 'DMEP.A0PD7193.DMECBID.SUPL.2011042981697.txt';

What's the alternative for this?

The perl script should go inside the folder

/etl/foldername/

and look for the file (In this case, it should be file pattern matching).
and then load the file...

The script that i am using and which does not find file is

#!/usr/bin/perl -w
$file = '/etl/PDAC/CBA/Supplier/';
$filepattern = '.*A0PD7193.*DMECBID.SUPL.*\.txt';
if ($file =~ $filepattern){
print "The file is there and now the Job load begins\n";
}else {
print "The file does not exist.\n";
}
exit;

This is the result. But the file does exist in the folder..

-bash-3.00$ ./Supplier_filecheck_andload_test1.pl
The file does not exist.

Appreciate any help...

If you want to list all files matching the pattern under /etl/PDAC/CBA/Supplier/, use the following script:

#!/usr/bin/perl
use strict;
use warnings;
opendir(DIR, "/etl/PDAC/CBA/Supplier/") or die "Failed.";
my @files = grep(/.*A0PD7193.*DMECBID.SUPL.*\.txt/, readdir DIR);
foreach my $f (@files) {
    print "$f\n"
}

You can also use bash:

#!/bin/bash

for f in `find /etl/PDAC/CBA/Supplier -maxdepth 1 -type f`; do
    if [ x`echo "$f" | grep '.*A0PD7193.*DMECBID.SUPL.*\.txt'` != "x" ]; then
        echo "The file is there and now the Job load begins."
    else
        echo "The file does not exist."
    fi
done
1 Like