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