Solved

Read multiple files dynamically

Posted on 2008-09-29
7
636 Views
Last Modified: 2012-05-05
I would like to merge script 1 and the script 2 into one. So that I could read multiple files from the control file.
Currenlty my control file has only 2 files listed, but in the future it could be more than 2. So I want my script to be also
dynamic. I've a script which reads the control file successfully and stores the name into @rFiles.


Script 1
-------

@ARGV=grep/^$rFiles[0].*\.txt$/i,@allfiles ;      #concatenating the first file read from control file
writeLog("argv 1 values are @ARGV\n");
my $outFile1 = $lFiles[0];
open STDOUT,">$workdir/$outFile1" or die( "$lFiles[0] $!");
print while <>;

Script 2
--------
@ARGV=grep/^$rFiles[1].*\.txt$/i,@allfiles ;      #concatenating the second file read from control file
writeLog("argv 2 values are @ARGV\n");
my $outFile2 = $lFiles[1];
open STDOUT,">$workdir/$outFile2" or die( "$lFiles[1] $!");
print while <>;
close;

sample control file
-------------------
file = hfoxxs.txt  : hfoxxs.dat  : hfoxxs.token  : hfoxxs.dsc
file = hfospsm.txt : ms_hfospsm.dat : ms_hfospsm.token : ms_hfospsm.dsc
.....
.....

I've written 2 scripts (hard coded) to concatenate the files which I read from the control file.

Script to read from the control file (works fine)
-----------------------------------------
$cmdfile = $ARGV[0])  || die "Missing controlfile argument. Usage: loaderftp.pl controlfile\n";
(-s $cmdfile )         || die "control file \"$cmdfile\" does not exist or has zero length\n";
open(CMDFILE,$cmdfile) || die "Unable to open file $cmdfile: $!\n";
while (<CMDFILE>)                                                   # read controlfile line by line
{   (/^\s*#/ || /^\s*$/) && next;                                   # skip comments and blanks
if (/^\s*file\s*=/)                                             # line contains a file= parameter?
{   /=\s*(\S+)\s*:\s*(\S+)\s*:\s*(\S+)\s*:\s*(\S+)\s*#*/;       # match on pattern file=name:name:name:name
       push(@rFiles, $1);push(@lFiles, $2);push(@tFiles, $3);push(@dFiles, $4); #read the files from control file
      }
else
{   die "Invalid parameter $_ \n";
}
}
close CMDFILE;
0
Comment
Question by:sventhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22600548
$rFiles=qr/^(?:@{[join"|",map quotemeta,@rFiles]}).*\.txt$/i;
@ARGV=grep/$rFiles/i,@allfiles ;
0
 
LVL 84

Expert Comment

by:ozo
ID: 22600595
Is the ".txt" part of $rFiles[0]?
grep/^$rFiles[0].*\.txt$/i would suggest that is is not, but
 /=\s*(\S+)\s*:\s*(\S+)\s*:\s*(\S+)\s*:\s*(\S+)\s*#*/ where $_ = 'file = hfoxxs.txt  : hfoxxs.dat  : hfoxxs.token  : hfoxxs.dsc
' would suggest that it is
0
 
LVL 18

Author Comment

by:sventhan
ID: 22600734
Thanks ozo.
.txt is not part of the rFiles[0].
The below code will remove the .txt just before the below script.
 $rFiles[0] =~ s/\.txt//g;  
Instead of calling this script twice I would like to keep at once.
I would really keep this as a sub routine and pass the parameter(s) to it.
@ARGV=grep/^$rFiles[0].*\.txt$/i,@allfiles ;      
my $outFile1 = $lFiles[0];
open STDOUT,">$workdir/$outFile1" or die( "$lFiles[0] $!");
print while <>;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:Adam314
ID: 22600941
Can you post your entire script?  It's hard to follow what you do with bits and pieces...
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 22601139
for( 1..@rFiles ){
  @ARGV=grep/^$rFiles[$_-1].*\.txt$/i,@allfiles ;      
 my $outFile1 = $lFiles[$_-1];
 open STDOUT,">$workdir/$outFile$_" or die( "$lFiles[$_-1] $!");
 print while <>;
}
0
 
LVL 18

Author Comment

by:sventhan
ID: 22605713
adam:
Please take a look at my entire script...
Thanks for your time.
ozo:
your script (below) works fine. The variable @rFiles has 2 files hfossm and hfospsm.when it loop it always hold the value for hfospsm and skips the first one.
Do I've to change the for loop from 0 .. @rFiles?
for( 1..@rFiles ){
  @ARGV=grep/^$rFiles[$_-1].*\.txt$/i,@allfiles ;      
 open STDOUT,">$workdir/$lFiles[$_-1]" or die( "$lFiles[$_-1] $!");
 print while <>;
}

loaderftp.pl.txt
0
 
LVL 18

Author Comment

by:sventhan
ID: 22605750
ozo:
Sorry!
I did a small mistake and i'll retest the code and let you know.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question