We help IT Professionals succeed at work.

Read multiple files dynamically

sventhan
sventhan asked
on
657 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;
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
$rFiles=qr/^(?:@{[join"|",map quotemeta,@rFiles]}).*\.txt$/i;
@ARGV=grep/$rFiles/i,@allfiles ;
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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

Author

Commented:
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 <>;
Top Expert 2009

Commented:
Can you post your entire script?  It's hard to follow what you do with bits and pieces...
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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

Author

Commented:
ozo:
Sorry!
I did a small mistake and i'll retest the code and let you know.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.