#!/usr/bin/perl -w -i.orig ################################################ # ReplaceMany.pl # Version 1 # Son Nguyen # UCSC Programming for Bioinformatics II # October 10, 2003 ################################################ # ReplaceMany will replace abc with xyz in all .fasta files # # Robert D. Cormia # October 10, 2003 # # After working on replace_many.pl on my own, I received # substantial help from Son Nguyen on this program. # # Write a Perl program called 'ReplaceMany' that will replace # all occurrences # of 'abc' to 'xyz' in all the data files # whose names end in '.fasta'. # use strict; # read file my $updateFileName = 'tempfasta'; my $line; # # open file, if open fails call die # foreach my $fileToRead (<*.fasta>) { #get the filename and start replacing its content print "$fileToRead\n"; open(INFILE, "$fileToRead") or die("open() failed for 'fname'\nReason: $!\n\n"); open(OUTFILE, ">$updateFileName") or die("open() failed for 'fname'\nReason: $!\n\n"); while ($line = ) { $line =~ s/abc/xyz/g; print OUTFILE $line; } close(INFILE); close(OUTFILE); rename("$updateFileName","$fileToRead") || die "can't rename file"; } print "Replaced abc by xyz. Content of all .fasta files within the current running directory has been changed!"; __END__ Replaced abc by xyz. Content of all .fasta files within the current running directory has been changed!