File: string-replace-list.pl

package info (click to toggle)
trilinos 16.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 994,012 kB
  • sloc: cpp: 3,764,859; ansic: 425,011; fortran: 160,684; python: 101,476; xml: 74,329; sh: 37,044; makefile: 22,641; perl: 7,525; f90: 6,424; csh: 5,285; objc: 2,620; lex: 1,646; lisp: 810; yacc: 603; javascript: 552; awk: 364; ml: 281; php: 145
file content (71 lines) | stat: -rwxr-xr-x 1,894 bytes parent folder | download
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl -w
#
# This perl script replaces a list of strings.
# Here it is allowed for file_in and
# file_out to be the same file.
#

use strict;

my $g_use_msg =
  "Use: string-replace-list.pl string_file file_in file_out\n";
if( scalar(@ARGV) != 3 ) {
  print STDERR $g_use_msg;
  exit(-1);
}

my $string_file   = shift;
my $file_in_name  = shift;
my $file_out_name = shift;

open STRING_NAMES_FILE, "<$string_file" || die "The file $string_file could not be opened!";
my @string_names_lines = <STRING_NAMES_FILE>;
my @string_names;
foreach(@string_names_lines) {
  my $line = $_;
  my @string_names_line_array = split(' ', $line);
  foreach(@string_names_line_array) {
    chomp($_);
    push @string_names, $_
  }
}

#print "file_in_name = $file_in_name\n";
if($file_in_name=~/CVS/) {
#  print "Do not replace in CVS\n";
  exit;
}
open FILE_IN, "<$file_in_name";
my @file_in_array = <FILE_IN>;
close FILE_IN;

#print $match_str . "\n";

my @file_out_array;
my $did_replacement = 0;
foreach(@file_in_array) {
  my $line = $_;
  #print "\n";
  #print "----------------------------------------------------\n";
  #print "\n";
  #print $line;
  my $i;
  for( $i=0; $i <scalar(@string_names); $i += 2 ) {
    my ($find_string, $replace_string) = ($string_names[$i], $string_names[$i+1]);
    defined($find_string) || die $!;
    defined($replace_string) || die $!;
    chomp($find_string);
    #print "find string = ${find_string}\n";
    chomp($replace_string);
    #print "replace string = ${replace_string}\n";
    #print "string_names line = $_";
    $did_replacement = 1 if $line=~s/$find_string/$replace_string/g;
    #print $line;
  }
  push @file_out_array, $line;
}
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;
}