![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| perl script on multiple files | aritakum | Shell Programming and Scripting | 4 | 05-21-2009 10:20 AM |
| Using sed with a foreach loop | Peggy White | Shell Programming and Scripting | 2 | 01-24-2009 09:37 AM |
| foreach loop | abch624 | Shell Programming and Scripting | 1 | 03-19-2008 09:34 PM |
| Combining Multiple files in one in a perl script | rahulrathod | Shell Programming and Scripting | 1 | 12-18-2005 01:51 AM |
| Perl - Iterating a hash through a foreach loop - unexpected results | quantumechanix | Shell Programming and Scripting | 5 | 12-15-2003 07:08 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Foreach loop to run a perl script on multiple files
Hi,
I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta alnCDS_2.fasta alnCDS_3.fasta alnCDS_4.fasta alnCDS_5.fasta alnCDS_6.fasta alnCDS_7.fasta alnCDS_8.fasta alnCDS_9.fasta alnCDS_10.fasta.... I am using the following foreach statement to run a perl script like so: foreach file1 ( *.aln ) ; foreach file2 ( *.fasta ) foreach? ./perl.script.pl $file1 $file2 > $file1$file2.out foreach? end This gives me output files 289620178.alnalnCDS_1.fasta.out 289620178.alnalnCDS_2.fasta.out 289620178.alnalnCDS_3.fasta.out ..... when what I want is the following: 289620178.alnalnCDS_1.fasta.out 289620179.alnalnCDS_2.fasta.out 289620180.alnalnCDS_3.fasta.out.... etc... so that each of the files (*.aln and *.fasta) are sequentially paired as input into the perl script without all of the possible pairs being run through. Is there a way to do this ? Thanks!! |
|
||||
|
That's because your two foreach loops are nested. So each file grabbed from the outer loop is paired with every file in the inner loop and then the next outer loop file gets grabbed and that paired with each file in the inner loop, etc.
The general solution to your problem would be to: 1. First load all the inner loop filenames into an array. 2. Now set up [what was] your outer loop. 3. For each file in your loop (now there's only one loop), take its name, and couple it with the next element in the array using either a counter, or pop, or what ever your script language allows. quine@sonic.net |
![]() |
| Bookmarks |
| Tags |
| foreach loops, multiple files, perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|