Solved

Read multiple files dynamically

Posted on 2008-09-29
7
607 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filtering a file to table 9 99
crawling gofundme 4 120
Executing multiple sybase statements in perl dbi 2 88
quoting a comma separated list 20 81
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now