1 |
#!/usr/bin/perl |
2 |
|
3 |
## ----------------------------------------------------------------------------------------- |
4 |
## $Id$ |
5 |
## ----------------------------------------------------------------------------------------- |
6 |
## $Log$ |
7 |
## |
8 |
## ----------------------------------------------------------------------------------------- |
9 |
|
10 |
use strict; |
11 |
use warnings; |
12 |
|
13 |
use Carp; |
14 |
use File::Find; |
15 |
use Getopt::Long; |
16 |
|
17 |
my $basedir; |
18 |
my $pattern_substitution; |
19 |
my $pattern_filename; |
20 |
my $verbose; |
21 |
my $dryrun; |
22 |
|
23 |
GetOptions( |
24 |
'basedir=s' => \$basedir, |
25 |
'file-pattern=s' => \$pattern_substitution, |
26 |
'substitution-pattern=s' => \$pattern_filename, |
27 |
'dry-run=s' => \$dryrun, |
28 |
#'interactive=s' => \$verbose, |
29 |
'verbose=s' => \$verbose, |
30 |
#'report=s' => \$verbose, |
31 |
'usage' => \&usage, |
32 |
'help' => \&usage, |
33 |
); |
34 |
|
35 |
sub usage { |
36 |
print <<EOU; |
37 |
|
38 |
replace.pl -- replaces contents in files by applying regular expression |
39 |
|
40 |
--substitution-pattern: give regex-pattern for substitution (e.g. "s/world/earth/") |
41 |
--file-pattern: |
42 |
--basedir |
43 |
|
44 |
EOU |
45 |
|
46 |
} |
47 |
|
48 |
|
49 |
my $pattern = shift; |
50 |
if (!$pattern) { |
51 |
print ("no pattern!"); |
52 |
#usage(); |
53 |
exit; |
54 |
} |
55 |
|
56 |
my $path = shift; |
57 |
if (!$path) { |
58 |
print ("no path!"); |
59 |
#usage(); |
60 |
exit; |
61 |
} |
62 |
|
63 |
my $pattern_file = '/CVS/Root'; |
64 |
|
65 |
sub replaceInFile { |
66 |
my $filename = shift; |
67 |
my $filename_bak = $filename . ".bak"; |
68 |
open(RH, $filename); |
69 |
open(WH, '>' . $filename_bak); |
70 |
while(<RH>) { |
71 |
eval($pattern); |
72 |
#print $_, "\n"; |
73 |
print WH $_; |
74 |
} |
75 |
close(WH); |
76 |
close(RH); |
77 |
|
78 |
unlink $filename; |
79 |
rename $filename_bak, $filename; |
80 |
|
81 |
} |
82 |
|
83 |
sub wanted { |
84 |
if ($File::Find::name =~ m/$pattern_file$/) { |
85 |
print $File::Find::name, "\n"; |
86 |
replaceInFile($File::Find::name); |
87 |
#exit; |
88 |
} |
89 |
} |
90 |
|
91 |
sub main { |
92 |
find(\&wanted, $path); |
93 |
} |
94 |
|
95 |
main(); |