-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplaceNames.pl
83 lines (67 loc) · 1.52 KB
/
replaceNames.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
my %opts;
getopts('f:hr:o:',\%opts);
&varcheck;
my $of = $opts{'o'} || "-";
open (RF,$opts{'r'}) or die "Can't open $opts{'r'}\n";
open (OUT, ">$of") or die "Can't open $opts{'o'}\n";
my %replacements;
while (my $line = <RF>){
chomp $line;
my ($target,$repl) = split("\t",$line);
$replacements{$target} = $repl;
}
close RF;
#foreach (keys %replacements){
# print "$_=$replacements{$_}\n";
#}
open (TF,$opts{'f'}) or die "Can't open $opts{'f'}\n";
while (my $line = <TF>){
chomp $line;
foreach my $target (keys %replacements){
my $sub = $replacements{$target};
#my $oldLine = $line;
$line =~ s/\Q$target\E/$sub/g;
#if($line =~ s/^\Q$target\E\t/$sub\t/ ||
#$line =~ s/>\Q$target\E/>$sub/){
#last;
#}
}
print OUT "$line\n";
}
close TF;
sub varcheck {
&usage if ($opts{'h'});
my $errors = "";
if (!$opts{'f'}){
$errors .= "-f flag not provided\n";
}
elsif(!(-e $opts{'f'})) {
$errors .= "Can't open $opts{'f'}\n";
}
if (!$opts{'r'}){
$errors .= "-r flag not provided\n";
}
elsif(!(-e $opts{'r'})) {
$errors .= "Can't open $opts{'r'}\n";
}
if ($errors ne "") {
print "\n$errors";
&usage;
}
}
sub usage{
my $scriptName = $0;
$scriptName =~ s/\/?.*\///;
print "\nusage: perl $scriptName <-f file> <-r file>\n";
print <<PRINTTHIS;
Replaces names in a file.
-f Target File
-r tab delimited name file with name to replace in the first column and replacement in second.
-o (optional) filename for output file
PRINTTHIS
exit;
}