By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,712 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,712 IT Pros & Developers. It's quick & easy.

Searching for Keywords in Files in a Dir

100+
P: 154
script to search for keywords in files in a dir

Basically the script consists of a file with part numbers

I got a dir with files I want to search the files line by line and if a line has the part number I want it to print that line to an outfile file.

I did this script but it does not find matching products.
I aded an else statement and it only prints the else statements alone when i commented out the else statements nothing prints tot output file.

This is the script i am working with hopefully someone could show where i am going wrong. Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $| = 1;
  3.  
  4. use File::Copy;
  5. # Contains a list of product numbers
  6. my $prodfile = "d:\\prod\\automated_Script\\testfile\\prod.txt";
  7.  
  8. #Contains a bunch of flat files where i would take a product number and check line in each file of that dir and if they have a line containing the prod number it will print to the output file.
  9. my $srcdir = "d:\\prod\\automated_Script\\archivetest";
  10. my $finalfile = "d:\\prod\\automated_Script\\testfile\\output.txt";
  11.  
  12. ##### Listing files in archive dir #####
  13. opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
  14. @files = grep {!/^\.+$/} readdir(DIR);
  15. close(DIR);
  16. print @files;
  17.  
  18. if (!@files) {
  19.     print "No files in dir.\n\n";
  20.     last;
  21. }
  22.  
  23. ##### Reading Line from File #####
  24. open(PRODFILE, "< $prodfile") or die (" Could not open Product File $!");
  25. while ($product = <PRODFILE>) {
  26.     chomp($product);
  27.     foreach $files (@files) {
  28.         print " This product is : $product \n";
  29.  
  30.         ### Open file and searching for matching products ###
  31.  
  32.         open(INFILE2,"< $srcdir\\$files") || die(" Could not open INFILE2 File!");
  33.         $findprod=<INFILE2>;
  34.         chomp($findprod);
  35.  
  36.         open(OUTFILE2, ">> $finalfile") || die ("Could not OUTFILE2 open file $!");
  37.  
  38.         while (<INFILE2>) {
  39.             if ($product =~ /$findprod/) {
  40.                 print OUTFILE2 "$findprod";
  41.             #} else {
  42.             #    print OUTFILE2 "Product not found \n";
  43.             }
  44.  
  45.             close OUTFILE2;
  46.         }
  47.         close INFILE2;
  48.  
  49.         print "Product search completed";
  50.     }
  51. }
  52. close PRODFILE;
  53.  
Feb 27 '07 #1
Share this Question
Share on Google+
11 Replies


KevinADC
Expert 2.5K+
P: 4,059
is this a one line file?

Expand|Select|Wrap|Line Numbers
  1.         open(INFILE2,"< $srcdir\\$files") || die(" Could not open INFILE2 File!");
  2.         $findprod=<INFILE2>;
  3.         chomp($findprod);
  4.  
Feb 27 '07 #2

100+
P: 154
no it has muliple lines
Feb 27 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
try this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. $| = 1;
  5.  
  6. use File::Copy;
  7. # Contains a list of product numbers
  8. my $prodfile = "d:\\prod\\automated_Script\\testfile\\prod.txt";
  9.  
  10. #Contains a bunch of flat files where i would take a product number and check line in each file of that dir and if they have a line containing the prod number it will print to the output file.
  11. my $srcdir = "d:\\prod\\automated_Script\\archivetest";
  12. my $finalfile = "d:\\prod\\automated_Script\\testfile\\output.txt";
  13.  
  14. ##### Listing files in archive dir #####
  15. opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
  16. my @files = grep {!/^\.+$/} readdir(DIR);
  17. close(DIR);
  18. print @files;
  19.  
  20. if (!@files) {
  21.     print "No files in dir.\n\n";
  22. }
  23.  
  24. ##### Reading Line from File #####
  25. open(PRODFILE, "< $prodfile") or die (" Could not open Product File $!");
  26. while (my $product = <PRODFILE>) {
  27.     chomp($product);
  28.     foreach my $files (@files) {
  29.         print " This product is : $product \n";
  30.  
  31.         ### Open file and searching for matching products ###
  32.  
  33.         open(INFILE2,"< $srcdir\\$files") || die(" Could not open INFILE2 File!");
  34.         open(OUTFILE2, ">> $finalfile") || die ("Could not OUTFILE2 open file $!");
  35.  
  36.       while (my $findprod = <INFILE2>) {
  37.          chomp($findprod);
  38.             if ($product =~ /$findprod/) {
  39.                 print OUTFILE2 "$findprod\n";
  40.             #} else {
  41.             #    print OUTFILE2 "Product not found \n";
  42.             }
  43.       }
  44.     }
  45.    print "Product search completed for: $product\n ";
  46. }
  47. close OUTFILE2;
  48. close INFILE2;
  49. close PRODFILE;
you can use forward slashes in your path statements even if you are using windows:

Expand|Select|Wrap|Line Numbers
  1. my $prodfile = 'd:/prod/automated_Script/testfile/prod.txt';
Feb 27 '07 #4

100+
P: 154
The script ran but it did not print the lines containing the matching part numbers.
When i searched the output file the part number could not be found in any of the lines. There are other products than the ones i was searching for.

Pretty strange.
Feb 27 '07 #5

100+
P: 154
Hi Kevin, It looks like the comparison string is not working , I did a print statement. The lines do not have anything relating to the product number i am trying to find. Is ther somthing wrong with my comparison string?

Expand|Select|Wrap|Line Numbers
  1. while (my $findprod = <INFILE2>) {
  2.          chomp($findprod);
  3.             if ($product =~ /$findprod/) {
  4.                 print OUTFILE2 "$findprod \n";
  5.                  print "******* Match Found: $findprod ********* \n\n"
try this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. $| = 1;
  5.  
  6. use File::Copy;
  7. # Contains a list of product numbers
  8. my $prodfile = "d:\\prod\\automated_Script\\testfile\\prod.txt";
  9.  
  10. #Contains a bunch of flat files where i would take a product number and check line in each file of that dir and if they have a line containing the prod number it will print to the output file.
  11. my $srcdir = "d:\\prod\\automated_Script\\archivetest";
  12. my $finalfile = "d:\\prod\\automated_Script\\testfile\\output.txt";
  13.  
  14. ##### Listing files in archive dir #####
  15. opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
  16. my @files = grep {!/^\.+$/} readdir(DIR);
  17. close(DIR);
  18. print @files;
  19.  
  20. if (!@files) {
  21.     print "No files in dir.\n\n";
  22. }
  23.  
  24. ##### Reading Line from File #####
  25. open(PRODFILE, "< $prodfile") or die (" Could not open Product File $!");
  26. while (my $product = <PRODFILE>) {
  27.     chomp($product);
  28.     foreach my $files (@files) {
  29.         print " This product is : $product \n";
  30.  
  31.         ### Open file and searching for matching products ###
  32.  
  33.         open(INFILE2,"< $srcdir\\$files") || die(" Could not open INFILE2 File!");
  34.         open(OUTFILE2, ">> $finalfile") || die ("Could not OUTFILE2 open file $!");
  35.  
  36.       while (my $findprod = <INFILE2>) {
  37.          chomp($findprod);
  38.             if ($product =~ /$findprod/) {
  39.                 print OUTFILE2 "$findprod\n";
  40.             #} else {
  41.             #    print OUTFILE2 "Product not found \n";
  42.             }
  43.       }
  44.     }
  45.    print "Product search completed for: $product\n ";
  46. }
  47. close OUTFILE2;
  48. close INFILE2;
  49. close PRODFILE;
you can use forward slashes in your path statements even if you are using windows:

Expand|Select|Wrap|Line Numbers
  1. my $prodfile = 'd:/prod/automated_Script/testfile/prod.txt';
Feb 27 '07 #6

KevinADC
Expert 2.5K+
P: 4,059
Your best bet is to add as many "print" lines as you can to "watch" the data and script as it's being processed. This often results in catching the error in the logic of code that runs but does not do what you expect it to.
Feb 27 '07 #7

100+
P: 154
After putting the print statements i think i troubleshoot it down to the if statement not work and it just prints all the lines from the extract files and not doing what the if statement said. IS there something I am doing wrong?

Expand|Select|Wrap|Line Numbers
  1.       while (my $findprod = <INFILE2>) {
  2.          chomp($findprod);
  3.             if ($product =~ /$findprod/) {
  4.                 print OUTFILE2 "$findprod \n";
  5.                  print "******* Match Found: $findprod ********* \n\n"}
  6.       }
Feb 27 '07 #8

KevinADC
Expert 2.5K+
P: 4,059
do this:

Expand|Select|Wrap|Line Numbers
  1.       while (my $findprod = <INFILE2>) {
  2.          chomp($findprod);
  3.          print qq~if ($product =~ /$findprod/)\n~; 
  4.             if ($product =~ /$findprod/) {
  5.                 print OUTFILE2 "$findprod \n";
  6.                  print "******* Match Found: $findprod ********* \n\n"}
  7.       }
and post some of the lins that get printed
Feb 27 '07 #9

100+
P: 154
Hi Kevin


Ok this is crazy I ran the code you gave just put the print statement quotes

Expand|Select|Wrap|Line Numbers
  1. qq~if (3CRTP0400C96C-ME =~ /EA|0223-000-151-1|PKG,END CAP,PROTEUS||||0.000|0.000|KG|0.000||0.000|0.000|0.000||90|N/A||| | ||/)
  2. ~******* Match Found: EA|0223-000-151-1|PKG,END CAP,PROTEUS||||0.000|0.000|KG|0.000||0.000|0.000|0.000||90|N/A||| | || ********* 
This is one of the lines it is printing fromt he extract file.

Now the number it is checking would be 0223-000-151-1 and as you can see 3CRTP0400C96C-ME does not match 0223-000-151-1. For some reason it comes up as a match. The only thing that did not match was the header but every other line showed as match which is wrong should only match about two lines that was in the extract but all lines are coming up as above.

I created a test extract file with the 3CRTP0400C96C-ME dummy lines and did not use the original extract format and the script worked.
as you could see below. I am puzzled what in the format of the orginal file could be causing it not to print the correct records only. Any ideas would be greatly appreciated.


Expand|Select|Wrap|Line Numbers
  1. qq~if (3CRTP0400CF96C-KR =~ /This is the products header/)
  2. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3CRTP0400C96C-ME|vfdggfgdfg|gfdgdfgf/)
  3. ~qq~if (3CRTP0400CF96C-KR =~ /EA|34RTP0400CF96C-KR|fdgfgdfgdfgdfgfd|fdgdfgdfgf/)
  4. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3SRTP0400C96C-ME|vfdggfgdfg|gfdgdfgf/)
  5. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3LRTP0400CF96C-KR|fdgfgdfgdfgdfgfd|fdgdfgdfgf/)
  6. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3URTP0400C96C-ME|vfdggfgdfg|gfdgdfgf/)
  7. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3HRTP0400CF96C-KR|fdgfgdfgdfgdfgfd|fdgdfgdfgf/)
  8. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3YRTP0400C96C-ME|vfdggfgdfg|gfdgdfgf/)
  9. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3HRTP0400CF96C-KR|fdgfgdfgdfgdfgfd|fdgdfgdfgf/)
  10. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3DRTP0400C96C-ME|vfdggfgdfg|gfdgdfgf/)
  11. ~qq~if (3CRTP0400CF96C-KR =~ /EA|3CRTP0400CF96C-KR|fdgfgdfgdfgdfgfd|fdgdfgdfgf/)
  12. ~******* Match Found: EA|3CRTP0400CF96C-KR|fdgfgdfgdfgdfgfd|fdgdfgdfgf ********* 
Feb 28 '07 #10

KevinADC
Expert 2.5K+
P: 4,059
looks to me like this:

Expand|Select|Wrap|Line Numbers
  1. if ($product =~ /$findprod/) {
should be:

Expand|Select|Wrap|Line Numbers
  1. if ($findprod =~ /\|$product\|/) {
or:

Expand|Select|Wrap|Line Numbers
  1. if ($findprod =~ /^\w+\|$product\|/) {
Feb 28 '07 #11

100+
P: 154
Thanks Kevin, I appreciate it that worked. :)
Feb 28 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.