File: string-replace.pl

package info (click to toggle)
trilinos 12.12.1-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 486,100 kB
  • sloc: cpp: 2,945,800; ansic: 419,979; fortran: 158,787; python: 38,880; xml: 38,271; sh: 16,290; f90: 7,531; makefile: 6,438; perl: 4,217; csh: 3,791; lex: 1,451; lisp: 810; yacc: 456; awk: 364; sed: 3
file content (43 lines) | stat: -rwxr-xr-x 1,088 bytes parent folder | download | duplicates (14)
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
#!/usr/bin/perl -w
#
# This perl script replaces a string with another string.
# Here it is allowd for file_in and file_out to be the
# same file.
#
use strict;
#
my $g_use_msg =
  "Use: string-replace.pl find_string replacement_string file_in file_out\n";
if( scalar(@ARGV) < 4 ) {
  print STDERR $g_use_msg;
  exit(-1);
}
#
my $find_string        = shift;
my $replacement_string = shift;
my $file_in_name       = shift;
my $file_out_name      = shift;
#
#
if($file_in_name=~/CVS/) {
#  print "Do not replace in CVS\n";
  exit;
}
#
open FILE_IN, "<$file_in_name" || die "The file $file_in_name could not be opended for input\n";
my @file_in_array = <FILE_IN>;
close FILE_IN;
#
my @file_out_array;
my $did_replacement = 0;
foreach(@file_in_array) {
  #print $_;
  $did_replacement = 1 if $_=~s/$find_string/$replacement_string/g;
  #print $_;
  push @file_out_array, $_;
}
if($did_replacement || $file_out_name ne $file_in_name) {
  open FILE_OUT, ">$file_out_name" || die "The file $file_out_name could not be opended for output\n";
  print FILE_OUT @file_out_array;
  close FILE_OUT;
}