The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-05-2009
greptastic greptastic is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 14
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!!
  #2 (permalink)  
Old 08-05-2009
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
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
Reply

Bookmarks

Tags
foreach loops, multiple files, perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0