Perl Pattern matching...

I am doing a file patterhn matching for a text file in PERL

I am using this,,, but it says that no file is found

$filepattern = '\d{1,4}.*A0NW9693.NDM.HBIDT.*.AD34XADJ.txt';

Can anyone help me out with Perl Pattern Matching concepts and how to do pattern matching for this txt file:

2011040675821.MBT.A0MW9693.NDM.HBIDT.AD38XADJ.txt

I want the script to look for '2011' and the rest will be the same?

Please help..

Try this,

echo '2011040675821.MBT.A0MW9693.NDM.HBIDT.AD38XADJ.txt' | perl -nle 'if(/^(\d{1,4})/) {print $1}'

Thanks for the quick reply, but i am not echoing here.

This is the perl script that i am using;

#!/oracle/product/11.2.0/perl/bin/perl -w
#set global variables
#directories where input file exist
$inputdirectories = '/MCS/MAINFRAME/REFERENCE/';
#directory where files will be moved to after job execution.
#$archivedirectory = '/MCS/MAINFRAME/REFERENCE/archive';
#command to move the files - to be used later.
#$mv_cmd = '/usr/bin/mv';
#Checking the server to connect
if ($ARGV[0] eq 'QA')
{
$ENV{"ORACLE_HOME"} = "/oracle/product/11.2.0";
$ENV{"PATH"} = "$ENV{'PATH'}:/oracle/product/11.2.0/bin";
$ENV{"ORACLE_SID"} = "db112";
#$dbsid = 'db112';
$dbuser = 'zx02169';
# $dbpwd = `/usr/local/pl/perlencrypt.pl -k /kda1/system/ealgorithm -d /sdr1/system/db112zx02169pw`;
}
 
$filepattern = '\d{1,4}.*A0NW9693.NDM.HBIDT.*.AD34XADJ.txt';

 
opendir ( IDIR, $inputdirectories) || die "Error in opening dir\n";
while( ($file = readdir(IDIR))){
        if ($file =~ $filepattern){
then
#Logfile location where it will reside
$logFile= "/MCS/MAINFRAME/REFERENCE/log/procedure_code.log";
#Control file location
$confile="/MCS/MAINFRAME/REFERENCE/control/proc_codes_34.ctl";
print "$dbuser\n";
#print "$dbpwd\n";
system ("/usr/local/pl/perlencrypt.pl -k /kda1/system/ealgorithm -d /sdr1/system/db112zx02169pw | sqlldr userid=$dbuser control=$confile log=$logFile");
print "Complete\n"
}else{
print "Cannot find the file\n";

Can you please show me how to use here in the script which uses this;

$filepattern = '\d{1,4}.*A0NW9693.NDM.HBIDT.*.AD34XADJ.txt';

---------- Post updated at 01:19 PM ---------- Previous update was at 01:01 PM ----------

I used the above said script;

echo '2011040675821.MBT.A0MW9693.NDM.HBIDT.AD38XPRC.txt' | perl -nle 'if(/^(\d{1,4})/) {print $1}'

and it errored out;

Unquoted string "echo" may clash with future reserved word at ./test_procedure_codes.pl line 27.
String found where operator expected at ./test_procedure_codes.pl line 27, near "echo '2011040675821.MBT.A0MW9693.NDM.HBIDT.AD38XPRC.txt'"
        (Do you need to predeclare echo?)
Unquoted string "perl" may clash with future reserved word at ./test_procedure_codes.pl line 27.
String found where operator expected at ./test_procedure_codes.pl line 27, near "nle 'if(/^(\d{1,4})/) {print $1}'"
        (Do you need to predeclare nle?)
Scalar found where operator expected at ./test_procedure_codes.pl line 38, near "$logFile"
        (Missing semicolon on previous line?)
syntax error at ./test_procedure_codes.pl line 27, near "echo '2011040675821.MBT.A0MW9693.NDM.HBIDT.AD38XPRC.txt'"
Unmatched right curly bracket at ./test_procedure_codes.pl line 48, at end of line
syntax error at ./test_procedure_codes.pl line 48, near "}"
Unmatched right curly bracket at ./test_procedure_codes.pl line 51, at end of line
Execution of ./test_procedure_codes.pl aborted due to compilation errors.

Try this,

$filepattern = '^\d{1,4}.*\.A0NW9693\.NDM.\HBIDT\.*\.AD34XADJ\.txt';

opendir ( IDIR, $inputdirectories) || die "Error in opening dir\n";
while( ($file = readdir(IDIR))){
        if ($file =~ /$filepattern/){
1 Like

Dont use #! /usr/bin/perl -w as the first line of your perl Programme with -w option
Instead Use- #! /usr/bin/perl

Once change rerun the Perl programme it should work.

Cheers
Rajiv