Solved

Read multiple files dynamically

Posted on 2008-09-29
7
598 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
Comment Utility
$rFiles=qr/^(?:@{[join"|",map quotemeta,@rFiles]}).*\.txt$/i;
@ARGV=grep/$rFiles/i,@allfiles ;
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 39

Expert Comment

by:Adam314
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ozo:
Sorry!
I did a small mistake and i'll retest the code and let you know.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

10 Experts available now in Live!

Get 1:1 Help Now