[2] | 1 | #! /usr/bin/perl -w |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | |
---|
| 6 | # converts all characters of one type into another |
---|
| 7 | # convert_characters.pl [input] [convert_from] [convert_to] [output] |
---|
| 8 | |
---|
| 9 | die "Check argument\n" unless @ARGV == 4; |
---|
| 10 | |
---|
| 11 | my $inputfile = $ARGV[0]; |
---|
| 12 | my $convert_from = $ARGV[1]; |
---|
| 13 | my $convert_to = $ARGV[2]; |
---|
| 14 | my $outputfile = $ARGV[3]; |
---|
| 15 | |
---|
| 16 | if ($convert_from eq "s") |
---|
| 17 | { |
---|
| 18 | $convert_from = '\s'; |
---|
| 19 | } |
---|
| 20 | elsif ($convert_from eq "T") |
---|
| 21 | { |
---|
| 22 | $convert_from = '\t'; |
---|
| 23 | } |
---|
| 24 | elsif ($convert_from eq "Sp") |
---|
| 25 | { |
---|
| 26 | $convert_from = '\s'; |
---|
| 27 | } |
---|
| 28 | elsif ($convert_from eq "Dt") |
---|
| 29 | { |
---|
| 30 | $convert_from = '\.'; |
---|
| 31 | } |
---|
| 32 | elsif ($convert_from eq "C") |
---|
| 33 | { |
---|
| 34 | $convert_from = ","; |
---|
| 35 | } |
---|
| 36 | elsif ($convert_from eq "D") |
---|
| 37 | { |
---|
| 38 | $convert_from = "-"; |
---|
| 39 | } |
---|
| 40 | elsif ($convert_from eq "U") |
---|
| 41 | { |
---|
| 42 | $convert_from = "_"; |
---|
| 43 | } |
---|
| 44 | elsif ($convert_from eq "P") |
---|
| 45 | { |
---|
| 46 | $convert_from = '\|'; |
---|
| 47 | } |
---|
| 48 | else |
---|
| 49 | { |
---|
| 50 | die "Invalid value specified for convert from\n"; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | if ($convert_to eq "T") |
---|
| 55 | { |
---|
| 56 | $convert_to = "\t"; |
---|
| 57 | } |
---|
| 58 | elsif ($convert_to eq "Sp") |
---|
| 59 | { |
---|
| 60 | $convert_to = '\s'; |
---|
| 61 | } |
---|
| 62 | elsif ($convert_to eq "Dt") |
---|
| 63 | { |
---|
| 64 | $convert_to = "\."; |
---|
| 65 | } |
---|
| 66 | elsif ($convert_to eq "C") |
---|
| 67 | { |
---|
| 68 | $convert_to = ","; |
---|
| 69 | } |
---|
| 70 | elsif ($convert_to eq "D") |
---|
| 71 | { |
---|
| 72 | $convert_to = "-"; |
---|
| 73 | } |
---|
| 74 | elsif ($convert_to eq "U") |
---|
| 75 | { |
---|
| 76 | $convert_to = "_"; |
---|
| 77 | } |
---|
| 78 | elsif ($convert_to eq "P") |
---|
| 79 | { |
---|
| 80 | $convert_to = "|"; |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | die "Invalid value specified for convert to\n"; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | my $fhIn; |
---|
| 88 | open ($fhIn, "< $inputfile") or die "Cannot open source file"; |
---|
| 89 | |
---|
| 90 | my $fhOut; |
---|
| 91 | open ($fhOut, "> $outputfile"); |
---|
| 92 | |
---|
| 93 | while (<$fhIn>) |
---|
| 94 | { |
---|
| 95 | my $thisLine = $_; |
---|
| 96 | chomp $thisLine; |
---|
| 97 | $thisLine =~ s/$convert_from{1,}/$convert_to/g; |
---|
| 98 | print $fhOut $thisLine,"\n"; |
---|
| 99 | } |
---|
| 100 | close ($fhIn) or die "Cannot close source file\n"; |
---|
| 101 | close ($fhOut) or die "Cannot close output fil\n"; |
---|