Solved

Read multiple files dynamically

Posted on 2008-09-29
7
619 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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 …
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…

825 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