1 |
joko |
1.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 |
|
|
croak("no pattern!") unless $pattern; |
51 |
|
|
|
52 |
|
|
my $path = shift; |
53 |
|
|
croak("no path!") unless $path; |
54 |
|
|
|
55 |
|
|
my $pattern_file = '/CVS/Root'; |
56 |
|
|
|
57 |
|
|
exit; |
58 |
|
|
|
59 |
|
|
sub replaceInFile { |
60 |
|
|
my $filename = shift; |
61 |
|
|
my $filename_bak = $filename . ".bak"; |
62 |
|
|
open(RH, $filename); |
63 |
|
|
open(WH, '>' . $filename_bak); |
64 |
|
|
while(<RH>) { |
65 |
|
|
eval($pattern); |
66 |
|
|
#print $_, "\n"; |
67 |
|
|
print WH $_; |
68 |
|
|
} |
69 |
|
|
close(WH); |
70 |
|
|
close(RH); |
71 |
|
|
|
72 |
|
|
unlink $filename; |
73 |
|
|
rename $filename_bak, $filename; |
74 |
|
|
|
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
sub wanted { |
78 |
|
|
if ($File::Find::name =~ m/$pattern_file$/) { |
79 |
|
|
print $File::Find::name, "\n"; |
80 |
|
|
replaceInFile($File::Find::name); |
81 |
|
|
#exit; |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
sub main { |
86 |
|
|
find(\&wanted, $path); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
main(); |