Pattern matching in Perl issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern matching in Perl issue
# 1  
Old 05-02-2011
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?
Code:
$filepattern = '.*A0PD7193.*DMECBID.SUPL.*\.txt';

The file name is
Code:
DMEP.A0PD7193.DMECBID.SUPL.2011042981697.txt

# 2  
Old 05-02-2011
Please try with double quotes

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

# 3  
Old 05-03-2011
The example as given (even without the double quotes, which change nothing in this case) works for me in this simple way:
Code:
#!/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.
# 4  
Old 05-03-2011
This pattern matching did work
Code:
my $filepattern = '.*A0PD7193.*DMECBID.SUPL.*\.txt';

The requirement is we get weekly files that will get loaded.
e.g.
Code:
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
Code:
my $filename    = 'DMEP.A0PD7193.DMECBID.SUPL.2011042981697.txt';

What's the alternative for this?

The perl script should go inside the folder
Code:
/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
Code:
#!/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..
Code:
-bash-3.00$ ./Supplier_filecheck_andload_test1.pl
The file does not exist.

Appreciate any help...
# 5  
Old 05-03-2011
If you want to list all files matching the pattern under /etl/PDAC/CBA/Supplier/, use the following script:
Code:
#!/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:
Code:
#!/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


Last edited by kevintse; 05-03-2011 at 01:24 PM..
This User Gave Thanks to kevintse For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - Use of *? in Matching Pattern

I am using Perl version 5.8.4 and trying to understand the use of regular expression. Following is my code and output. $string = "Perl is a\nScripting language"; ($start) = ($string =~ /\A(.*?) /); @lines = ($string =~ /^(.*?) /gm); print "First Word (using \\A): $start\n","Line... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

2. Shell Programming and Scripting

issue with pattern matching

i have 2 strings with values below which are read from a file 1 ---> end 2 ---> string(1)newline="\n"; which need to be compared inside an if block as below if \\n\"\;" ] then echo "pattern match" fi but the above code is not working (1 Reply)
Discussion started by: cvsanthosh
1 Replies

3. Shell Programming and Scripting

Need help with perl pattern matching

My log file looks as given below, its actually a huge file around 1 GB and these are some of the line: conn=5368758 op=10628050 msgId=64 - RESULT err=0 tag=101 nentries=1 etime=0 conn=7462122 op=-1 msgId=-1 - fd=247 slot=247 LDAPS connection from 10.13.18.12:37645 to 10.18.6.45 conn=7462122... (5 Replies)
Discussion started by: sags007_99
5 Replies

4. Shell Programming and Scripting

Pattern Matching in PERL

I have a 2 files in .gz format and it consists of 5 million lines the format of the file would be gzcat file1.gz | more abcde aerere ffgh56 .. .. 12345 gzcat file2.gz | more abcde , 12345 , 67890, ffgh56 , 45623 ,12334 whatever the string is in the file1 should be matched... (3 Replies)
Discussion started by: aravindj80
3 Replies

5. Shell Programming and Scripting

awk pattern matching and shell issue.

Please help me in this issue. I am unable to get the job,seems the awk not browsing the files. Please find my tries below. I have attached two files : 1.tobesearched.txt - a glimpse of a huge log file. 2.searchstring.txt - searching keys. these are the two scripts i tried writing: ... (7 Replies)
Discussion started by: deboprio
7 Replies

6. Shell Programming and Scripting

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:... (4 Replies)
Discussion started by: msrahman
4 Replies

7. Shell Programming and Scripting

Perl pattern matching!!

Hi experts, I have many occurances of the following headers in a file. I need to grep for the word changed/inserted in the header, calculate the difference between the two numbers and list the count incrementally. Headers in a file look like this: ------------------- ---------------------... (6 Replies)
Discussion started by: nmattam
6 Replies

8. Shell Programming and Scripting

Perl Pattern Matching

Hello experts, I have a file containing the following text(shortened here). File Begin ---------- < # Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 < Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 --- > # Billboard.d3fc1302a677.imagePath=S:\\efcm_Cassini >... (2 Replies)
Discussion started by: nmattam
2 Replies

9. Shell Programming and Scripting

Issue with Pattern Matching in Unix

Hi, I am trying to replace a specific column values in a csv file with double quotes. Example: SNO,NAME,ZIPCODE,RANK,CARE_OF 1,Robert,74538,12,RICHARD JOHNSON, P.C 2,Sam,07564,13,% R.S MIKE, V.K.S 3,Kim, Ed,12345,14,@90 KMS, %TK Desired Output: SNO,NAME,ZIPCODE,RANK,CARE_OF... (1 Reply)
Discussion started by: techmoris
1 Replies

10. Shell Programming and Scripting

Perl -Pattern Matching help..!

Hi, I got doubt in Pattern matching, could you tell me how the following differs in action ?? if ( $line1==/$line2/ ) if ( $line1=~/$line2/ ) if ( $line1=~m/$line2/) What is the significance of '~' in matching. Thanks in advance CoolBhai (5 Replies)
Discussion started by: coolbhai
5 Replies
Login or Register to Ask a Question