This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
search_and_replace.pl
executable file
·84 lines (73 loc) · 1.66 KB
/
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
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
84
#!/usr/bin/perl
# This file is part of Adblock Plus <https://adblockplus.org/>,
# Copyright (C) 2006-present eyeo GmbH
#
# Adblock Plus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# Adblock Plus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
use strict;
my $exec = 0;
for (my $i = 0; $i < @ARGV; $i++)
{
if ($ARGV[$i] eq "-e")
{
$exec = 1;
splice(@ARGV, $i--, 1);
}
}
die "Usage: $^X $0 [-e] <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;
if ($exec)
{
$count = ($data =~ s/$from/$to/gee);
}
else
{
$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);
}
}