#!/usr/bin/perl # ----------------------------------------------------------------------- # replaceX.pl 1.0 # # This programm replace character A with B from an input textfile # and writes the result to an output textfile. # # For example it is possible to replace all A,C,T,G with the value '1' # and all X and N with value '0'. # # The command line for this is: # ./replaceX.ps input.txt output.txt 'X|N' 0 'A|C|T|G' 1 # # # Copyright by Joern Hameister (2005) # ----------------------------------------------------------------------- if(@ARGV<4) { print "This programm replace character A with B from an input textfile and writes the result to an output textfile.\nParameter 1: Inputfile\nParameter 2: Outputfile\nParameter 3: Character A\nParameter 4: Character B\nFor example: ./replaceX.pl ./inputfile.txt ./outputfile.txt A B\nRegular expressions in the parameters are possible too.\nExample: ./replaceX.pl ./inputfile.txt ./outputfile.txt X 0 'A|C|T|G' 1\nThis command replace all X with 0 and all A or C or T or G with 1."; } elsif(@ARGV%2==1) { print "Number of parameters must be odd.\n"; } else { print "Read from @ARGV[0] (Inputfile)\n"; print "Write to @ARGV[1] (Outputfile)\n"; open(INPUT , "<", @ARGV[0]); open(SINK , ">" , @ARGV[1]); while() { for($i= 2; $i < @ARGV;) { $char1 = @ARGV[$i]; $char2 = @ARGV[$i+1]; $i += 2; # print "Replace $char1 with $char2\n"; s/$char1/$char2/g; } print SINK $_; } }