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

Searching a lot of files

P: 2
Hi all:

I am new to PerlScripting .I got an assignment for parsing number of files at one time.The scenario is that I have 5 txt files in the same folder with different names (like SA1,XA2 etc.).I have to go through all these files and search for a pattern N008710LP in all the files and then stream the whole which contains this pattern to a new file which I have to create in the same script (in the format of Issuesmmddyyyy.txt).Can anyone give me a startup on that I will be very thankful.

Thanks
Jan 24 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. my @files =qw(foo.txt bar.txt blah.txt);
  2. foreach my $file (@files) {
  3.    put Travis code here from Tek-Tips
  4. }
Jan 24 '08 #2

P: 2
Hi KevinD:

Actually what I am trying to do here is to search a large number of files for a pattern NA001 in the folder /home/pp/.I
have many .txt files in the pp folder. I came up with the follwing code which will goto the pp folder and get the
name of the txt files in the pp folder and stores those @filenames array .This far is running good as I already tested it.
Now comes the searching part I am really stuck here I want my script here search each text file in my array @filenames in
the pp folder for the NA001 and the print filename and the whole line .Please see the code below.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. opendir(CDIR,".");
  4.  
  5. @filenames = grep(/\.txt/,readdir(CDIR));
  6. @filenames=sort(@filenames);
  7. closedir(CDIR);
  8.  
  9.  
  10. foreach my $file(@filenames)) {
  11. open(FILE,"./pp/") or die "Can't open path ";
  12. while(<FILE>){
  13. if (/NA001/) {
  14. print "$_\n";
  15. }
  16. close FILE;
  17. }
  18.  
I will appreciate your help.

Thanks,

A Perl Beginner :)
Jan 25 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
The biggest problem is that you have not done anything with the $file variable in the "foreach" loop. I am not sure where $file goes in this path:

open(FILE,"./pp/")


but just plug it in where ever it goes and the script should work. I have fixed one syntax error and rewritten the script to compile with "strict" turned on:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. # use these two pragmas from now on!
  3. use strict;
  4. use warnings;
  5.  
  6. # this part is good
  7. opendir(CDIR,".");
  8. my @filenames = grep(/\.txt/,readdir(CDIR));
  9. @filenames = sort(@filenames);
  10. closedir(CDIR);
  11.  
  12.  
  13. # here you didn't do anything with $file
  14. # and you had at least one syntax error 
  15. # plug $file into the path and run the script
  16. foreach my $file (@filenames) {
  17.     open(FILE,"./pp/") or die "Can't open path ";
  18.     while(<FILE>){
  19.         if (/NA001/) {
  20.             print "$_\n";
  21.         }
  22.     }
  23.     close FILE;
  24. }
Jan 26 '08 #4

Post your reply

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