1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # $Id$ |
---|
4 | # |
---|
5 | |
---|
6 | eval "exec /usr/local/bin/perl -S $0 $*" |
---|
7 | if $running_under_some_shell; |
---|
8 | |
---|
9 | require 'getopts.pl'; |
---|
10 | &Getopts("p:vh"); # help, pattern file, verbose, |
---|
11 | |
---|
12 | if ($opt_h || ! $opt_p) { |
---|
13 | print STDERR <<NO_MORE_HELP; |
---|
14 | word-replace2 |
---|
15 | |
---|
16 | Replace *words* with patterns. Pattern file specifies which patterns |
---|
17 | to replace on each line. All patterns are wrapped with perl \\b regexp |
---|
18 | specifiers. |
---|
19 | |
---|
20 | Usage: $0 [-v] -p pattern-file files to replace |
---|
21 | |
---|
22 | -v -- possibly more verbose |
---|
23 | -p file -- pattern file |
---|
24 | -h -- help |
---|
25 | |
---|
26 | anything else == this help message |
---|
27 | |
---|
28 | Pattern file looks like this: |
---|
29 | |
---|
30 | # Example: |
---|
31 | # ignores all lines with beginning with # or not exactly 2 fields |
---|
32 | _Dorky_Name rtems_dorky_name # comments, and blank lines are cool |
---|
33 | _Dorky_Name2 rtems_dorky_name2 # comments, and blank lines are cool |
---|
34 | NO_MORE_HELP |
---|
35 | exit 0; |
---|
36 | } |
---|
37 | |
---|
38 | $verbose = $opt_v; |
---|
39 | $pattern_file = $opt_p; |
---|
40 | |
---|
41 | # make standard outputs unbuffered (so the '.'s come out ok) |
---|
42 | $oldfh = select(STDERR); $| = 1; select($oldfh); |
---|
43 | $oldfh = select(STDOUT); $| = 1; select($oldfh); |
---|
44 | |
---|
45 | # pull in the patterns |
---|
46 | open(PATTERNS, "<$pattern_file") || |
---|
47 | die "could not open $pattern_file: $!, crapped out at"; |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | foreach (<PATTERNS>) |
---|
52 | { |
---|
53 | chop; |
---|
54 | s/#.*//; |
---|
55 | next if /^$/; |
---|
56 | ($orig, $new, $junk, @rest) = split; |
---|
57 | next if ( ! $orig || ! $new || $junk); # <2 or >2 patterns |
---|
58 | die "pattern appears 2x: '$orig' in '$pattern_file'--" if defined($patterns{$orig}); |
---|
59 | $patterns{$orig} = $new; |
---|
60 | } |
---|
61 | close PATTERNS; |
---|
62 | # walk thru each line in each file |
---|
63 | |
---|
64 | $infile = '-' ; |
---|
65 | $outfile = '-' ; |
---|
66 | |
---|
67 | if ( $#ARGV > -1 ) |
---|
68 | { |
---|
69 | $infile = "@ARGV[0]" ; |
---|
70 | shift @ARGV ; |
---|
71 | } |
---|
72 | |
---|
73 | if ( $#ARGV > -1 ) |
---|
74 | { |
---|
75 | $outfile = "@ARGV[0]" ; |
---|
76 | shift @ARGV ; |
---|
77 | } |
---|
78 | |
---|
79 | open (INFILE, "<$infile") || |
---|
80 | die "could not open input file $infile: $!"; |
---|
81 | $line = join('',<INFILE>) ; |
---|
82 | close INFILE; |
---|
83 | |
---|
84 | |
---|
85 | print STDERR "$outfile\t"; |
---|
86 | |
---|
87 | open (OUTFILE, ">$outfile") || |
---|
88 | die "could not open output file $outfile: $!"; |
---|
89 | |
---|
90 | foreach $key (keys %patterns) |
---|
91 | { |
---|
92 | if ( $line =~ s/\b$key\b/$patterns{$key}/ge ) |
---|
93 | { |
---|
94 | print STDERR "." ; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | print OUTFILE $line ; |
---|
99 | |
---|
100 | print STDERR "\n"; |
---|
101 | close OUTFILE; |
---|