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

Help with file search

P: 3
Hi, I am new to Perl. I need help with file search for the following scenario.
Currently as part of the archiving process, we have archived the files under
/$rootdir/Archive/yyyy directory where yyyy is year.
During the archiving process, for each day, yyyymmdd_trn.lst file and yyyymmdd_trn.tar.gz files are created where lst file will contain the names of all files that have been archived under yyyymmdd_trn.tar.gz file.

I am in the process of extracting (gunzip) given file from appropriate .tar.gz file into /$rootdir/Archive/Extract directory for reuse.

I am able to extract the required file, when I know the archive date.
The problem is when I don't know the archive date, I will be able to search the file name that I want to extract and identify the .lst file name.
Eg: The file that I need to extract is abc999877777.xml
This name should be in contents of one of the .lst file under /$rootdir/Archive/yyyy
I want to search contents of all .lst file under Archive directry for match abc999877777.xml to identify which year (yyyy) directory and name of the .lst file.

Can you help me to achive this? Quick response is appreciated? Thanks.
Regards,
Sutharsan
Aug 11 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Can we see what code you have tried thus far to do this? We can help you to get it working once we can see your attempt(s).

Regards,

Jeff
Aug 11 '08 #2

P: 3
Can we see what code you have tried thus far to do this? We can help you to get it working once we can see your attempt(s).

Regards,

Jeff
Thanks Jeff for the quick response.
Here is a section of the code that involved with the extract.

Expand|Select|Wrap|Line Numbers
  1. sub Process($)
  2. {
  3.     my($list) = @_;
  4.     my($RC,$ArchiveDIR,$DestDIR,$ArchType,$ListFile,$ArchFileFnd);
  5.  
  6.     $RC=0;
  7.  
  8.     # Setup.  Work out the archive and destination directories
  9.     #         and the changeable part of the archive name.
  10.     if ( $ExtractPath eq "$TRANSDIR" && $ExtractFile ) {
  11.         $ArchiveDIR="$TRANSDIR/Archive";
  12.         $DestDIR="$TRANSDIR/Archive/Extract";
  13.         $ArchType="Trn";
  14.     } elsif (  $ExtractPath eq "$MSGDIR" && $ExtractFile ) {
  15.         $ArchiveDIR="$MSGDIR/Archive";
  16.         $DestDIR="$MSGDIR/Archive/Extract";
  17.         $ArchType="Msg";
  18.     } elsif ( $ExtractPath ne "$TRANSDIR" && $ExtractPath ne "$MSGDIR" ) {
  19.         print "Error:Currently only Transactions and Messages Archiving allowed. \n";
  20.         print "For other Archiving contact system admin. \n";
  21.         $RC=4;
  22.     } else {
  23.        print "Error: Unknown error encountered during Archiving extract. \n";
  24.        $RC=8;
  25.     }
  26.  
  27.     $ListFile="$ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.lst";
  28.     print "$DestDIR \n";
  29.     print "$ListFile \n";
  30.  
  31.     if ( -e $ListFile ) {
  32.         print "$ListFile exists \n";
  33.         $ArchFileFnd="Y";
  34.     } else {
  35.        chdir ($ArchiveDir);
  36.     # This is the slot where I need the help.
  37.     # I have to search for the location and the name of a .lst of that contains $ExtractFile name. 
  38.     # This will lead me to the tar.gz file for extract.
  39.     #  
  40.  
  41.     #  print "$ListFile does not exist \n";
  42.     }
  43.  
  44.    if ( $ArchFileFnd ) {
  45.        chdir($DestDIR);
  46.       if ( -f "$ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz" ) {
  47.          system("gunzip -c $ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz | tar xvf - $ExtractFile");
  48.       } else {
  49.              print "Error: Archive missing: $ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz\n";
  50.              $RC=8;
  51.              }
  52.  
  53.       if  ( $RC == 0 && ! -f "${ExtractFile}" ) {
  54.           # Still no filename after extract. Return a warning
  55.           print "${ExtractFile} not found after the extract";
  56.           $RC=4;
  57.           }
  58.    }
Aug 12 '08 #3

nithinpes
Expert 100+
P: 410
I am not sure if I understood your requirement completely. You can make use of File::Find module to recursively search inside a directory.
If you want to search all .lst files inside all year directories, for a particular file in the list ($ExtractFile) and get the list of target zip files, you can make use of this piece of code.
Expand|Select|Wrap|Line Numbers
  1. use File::Find;
  2. #
  3. #
  4. #
  5. my @targets;
  6.  
  7. find( 
  8.   sub { 
  9.      my $file =$File::Find::name;#contains entire path of file
  10.      if((-f $file) && $file=~/\.lst/) {
  11.      open(F,"$file") or die "error:$!";
  12.     my @file= <F>;
  13.     if(grep /$ExtractFile/,@file){
  14.       my $reqyear = $1 if($file=~/\/(\d{4})\/.+?$/);##get the year
  15.       $file=~s/\.lst/\.tar\.gz/;  # get the target zip file
  16.       push @targets,$file; # get the list of targets
  17.            }
  18.        }  }, 
  19.     $ArchiveDIR); ## recursively search in $ArchiveDIR
  20.  
  21.  
Aug 12 '08 #4

P: 3
Thanks Nithin,

Implemented your suggested code and it works perfectly.

Thank you again for your help.

Regards,
Sun


I am not sure if I understood your requirement completely. You can make use of File::Find module to recursively search inside a directory.
If you want to search all .lst files inside all year directories, for a particular file in the list ($ExtractFile) and get the list of target zip files, you can make use of this piece of code.
Expand|Select|Wrap|Line Numbers
  1. use File::Find;
  2. #
  3. #
  4. #
  5. my @targets;
  6.  
  7. find( 
  8.   sub { 
  9.      my $file =$File::Find::name;#contains entire path of file
  10.      if((-f $file) && $file=~/\.lst/) {
  11.      open(F,"$file") or die "error:$!";
  12.     my @file= <F>;
  13.     if(grep /$ExtractFile/,@file){
  14.       my $reqyear = $1 if($file=~/\/(\d{4})\/.+?$/);##get the year
  15.       $file=~s/\.lst/\.tar\.gz/;  # get the target zip file
  16.       push @targets,$file; # get the list of targets
  17.            }
  18.        }  }, 
  19.     $ArchiveDIR); ## recursively search in $ArchiveDIR
  20.  
  21.  
Aug 13 '08 #5

Post your reply

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