source: CIVL/mods/dev.civl.com/scripts/sub.pl@ 1aaefd4

main test-branch
Last change on this file since 1aaefd4 was aad342c, checked in by Stephen Siegel <siegel@…>, 3 years ago

Performing huge refactor to incorporate ABC, GMC, and SARL into CIVL repo and use Java modules.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5664 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100755
File size: 1.3 KB
Line 
1#!/usr/bin/perl
2use File::Temp;
3
4sub substitute {
5 my($oldstring) = $_[0];
6 my($newstring) = $_[1];
7 my($filename) = $_[2];
8 my($line);
9 my($found) = 0;
10
11 my($tmpfile) = "PERLSUBTMPFILE";
12 open(OLDFILE, "<$filename") || die "Could not open $filename";
13
14 while (!$found && defined($line=<OLDFILE>)) {
15 if ($line =~ /$oldstring/) {
16 print "String found in $filename: $line";
17 print "Substituting within this file\n\n";
18 $found = 1;
19 }
20 }
21 close(OLDFILE) || die "Could not close $filename";
22
23 if ($found) {
24 open(OLDFILE, "<$filename") || die "Could not open $filename";
25 open(NEWFILE, ">$tmpfile") || die "Could not open $tmpfile";
26 while (defined($line=<OLDFILE>)) {
27 $line =~ s/$oldstring/$newstring/g;
28 print NEWFILE $line;
29 }
30 close(OLDFILE) || die "Could not close $filename";
31 close(NEWFILE) || die "Could not close $newfile";
32 unlink($filename) || die "Could not delete $filename";
33 rename $tmpfile, $filename || die "Could not rename $tmpfile to $filename";
34 }
35}
36
37$numargs = $#ARGV + 1;
38# print "num args is $numargs\n";
39# print "arg0: $ARGV[0]\t arg1: $ARGV[1]\n";
40die "At least 3 arguments required: old string, new string, one or more file names"
41 unless $numargs >= 3;
42foreach $file (@ARGV[2..$#ARGV]) {
43 printf("Examining file $file...\n");
44 &substitute($ARGV[0], $ARGV[1], $file);
45}
461;
Note: See TracBrowser for help on using the repository browser.