Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process Archived Files

Status
Not open for further replies.

fmuquartet

Programmer
Sep 21, 2006
60
0
0
US
Greetings!
I have a new requirement for a small shell scrip that I want to convert over to perl as follows:

1)unzip files in the format 'file-`date +%Y%m%d`.zip' into Spool DIR

2)parse each file with a perl script and append prefix ADD on them
e.g ADD_file-`date +%Y%m%d`.zip

3) package them up in an zip archive into /var/tmp/file-`date +%Y%m%d`.zip

Presentlty, I am simply unzipping the files and passing them to perl script as follows:

unzip -p file-`date +%Y%m%d`.zip |perlscript.pl > file-`date +%Y%m%d`.txt

Note: I need to have each file in the archive parsed against the perl script and ADD prefixed now.

If someone can help me out here, is is kindly apprciated!
 
MikeLacey--
Initally, your example is how I was doing things, but the requirements changed where I needed individual files parsed, instead of one file containing the parsed data.

So no I am passing the data in a for loop:

for file in `ls -1 *.txt`
do
cat $i |perlscript.pl > ADD_$i-`date +%Y%m%d`.txt



Would much rather have a perl script.
 
Oh ok, I see - well, as you would imagine, the Perl code for what you want to do looks similar(ish) to your existing code.

# for each input file
while (<*.txt>) {
# open it
open(IN,$_) || die;
# create a target file
open(OUT,">ADD_filename.txt") || die;
# read each line of the input file
while(<IN>){
# parse and process it
# then write your data out to the target file
print OUT $_;
}
# then close the target file
close(OUT);
# and the input file, best practise, believe me
close(IN);
}

You can get the date stuff you need for the target filenames from here:
Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top