forked from lovelywcm/autoproxy
-
Notifications
You must be signed in to change notification settings - Fork 56
/
search_and_replace.pl
52 lines (42 loc) · 803 Bytes
/
search_and_replace.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
#!/usr/bin/perl
use strict;
die "Usage: $^X $0 <regexp> <replaceBy>\n" unless @ARGV >= 2;
my ($from, $to) = @ARGV;
doDir('.');
sub doDir
{
my $dir = shift;
opendir(local *DIR, $dir) or die "Could not open directory $dir";
foreach (readdir(DIR))
{
next if !/[^.]/;
my $path = "$dir/$_";
if (-f $path)
{
doFile($path);
}
elsif (-d $path)
{
doDir($path);
}
}
closedir(DIR);
}
sub doFile
{
my $file = shift;
print "$file\n";
open(local *FILE, $file) or die "Could not read file $file";
binmode(FILE);
local $/;
my $data = <FILE>;
my $count = ($data =~ s/$from/$to/g);
close(FILE);
if ($count)
{
open(FILE, ">$file") or die "Could not write file $file";
binmode(FILE);
print FILE $data;
close(FILE);
}
}