#!/usr/bin/perl -w ################################################ # ReplaceOne.pl # Version 1 # Robert D. Cormia # UCSC Programming for Bioinformatics II # October 10, 2003 ################################################ # ReplaceOne will replace abc with xyz in a single file # # Robert D. Cormia # October 10, 2003 # # Write a Perl program called 'ReplaceOne' that will replace # all occurrences of 'abc' to 'xyz' in a data file called 'data1' # use strict; # read file my $fname_input = 'data1.txt'; my $fname_output = 'output.txt'; my $line; my $date; print "This program will open a text file and convert abc to xyz."; print "\n"; # # open file, if open fails call die # open(INPUT_HANDLE, "$fname_input") or die("open() failed for 'fname'\nReason: $!\n\n"); open(OUTPUT_HANDLE, ">$fname_output") or die("open() failed for 'fname'\nReason: $!\n\n"); # If a file is passed as an argument to this script, # user input will not be expected but the file # provided will be automatically read. You can # provide as many files as you want - all of them # will read in the order they were provided. # Process one line (change abc to xyz) while ($line = ) { $line =~ s/abc/xyz/g; print OUTPUT_HANDLE $line; } close(INPUT_HANDLE); close(OUTPUT_HANDLE); rename("$fname_output","$fname_input") || die "can't rename outfile.txt"; print "This program has converted abc to xyz."; __END__ Replaced abc by xyz in data1.txt