Visit Our UNIX and Linux User Community


Foreach loop to run a perl script on multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Foreach loop to run a perl script on multiple files
# 1  
Old 08-05-2009
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  
Old 08-05-2009
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
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #597
Difficulty: Easy
C does not supports do-while loops.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print perl hash value in foreach loop

Experts - Any advice on how to get a hash value in a foreach loop? Values print correctly on standalone print statements, but I can't access value in foreach loop. See sample code below and thanks in advance. foreach my $z (sort keys %hash) { for $y (@{$hash{$z}}) { print "$z... (6 Replies)
Discussion started by: timj123
6 Replies

2. Shell Programming and Scripting

How to use a loop for multiple files in a folder to run awk command?

Dear folks I have two data set which there names are "final.map" and "1.geno" and look like this structures: final.map: gi|358485511|ref|NC_006088.3| 2044 gi|358485511|ref|NC_006088.3| 2048 gi|358485511|ref|NC_006088.3| 2187 gi|358485511|ref|NC_006088.3| 17654 ... (2 Replies)
Discussion started by: sajmar
2 Replies

3. Shell Programming and Scripting

Run one script on multiple files and print out multiple files.

How can I Run one script on multiple files and print out multiple files. FOR EXAMPLE i want to run script.pl on 100 files named 1.txt ....100.txt under same directory and print out corresponding file 1.gff ....100.gff.THANKS (4 Replies)
Discussion started by: grace_shen
4 Replies

4. UNIX for Dummies Questions & Answers

Run one script on multiple files and print out multiple files.

How can I run the following command on multiple files and print out the corresponding multiple files. perl script.pl genome.gff 1.txt > 1.gff However, there are multiples files of 1.txt, from 1----100.txt Thank you so much. No duplicate posting! Continue here. (0 Replies)
Discussion started by: grace_shen
0 Replies

5. Shell Programming and Scripting

How to run perl script on multiple files of two directories?

Hi I have 100 files under file A labled 1.txt 2.txt.....100.txt(made up name) I have 1 files under file B labled name.txt How can i run the same perl script on 100 files and file name.txt I want to run perl script.pl A/1.txt B/name.txt perl script.pl A/2.txt B/name.txt ....... perl... (3 Replies)
Discussion started by: grace_shen
3 Replies

6. Shell Programming and Scripting

foreach loop in unix script

Hi all, I have a script which searches for all sql files in the current directory and replaces all sql files with an underscore with a dash. The next part I need to do is record the number of changes made (underscore to dash) and display this value (e.g.2). This is what I have so far; find /... (17 Replies)
Discussion started by: shawi
17 Replies

7. Shell Programming and Scripting

Run perl script with multiple file arguments

Hello everyone, I have two types of files in a directory: *.txt *.info I have a perl script that uses these two files as arguments, and produces a result file: perl myScript.pl abc.txt abc.xml How can I run this script (in a "for" loop , looping through both types of files)... (4 Replies)
Discussion started by: ad23
4 Replies

8. Shell Programming and Scripting

Run perl script on files in multiple directories

Hi, I want to run a Perl script on multiple files, with same name ("Data.txt") but in different directories (eg : 2010_06_09_A/Data.txt, 2010_06_09_B/Data.txt). I know how to run this perl script on files in the same directory like: for $i in *.txt do perl myscript.pl $i > $i.new... (8 Replies)
Discussion started by: ad23
8 Replies

9. Programming

PERL, search and replace inside foreach loop

Hello All, Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location. CODE: .. .. ... foreach $key(keys %arr_hash) { my ($loc,$ind,$add) = split /,/, $arr_hash{$key}; &create_verilog($key, $loc, $ind ,$add); } sub create_verilog{... (2 Replies)
Discussion started by: riyasnr007
2 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies

Featured Tech Videos