I wrote a perl script to replace a line
of javascript code I have in over 50 files,
but since there are single quotes in it, I am having
trouble. any suggestions would be great.
Line I'm trying to change
addItem(' Solutions', ' 'top');
to:
addItem(' Solutions', ' 'top');
I'm envoking the code this way:
C:\test>perl textchanger.perl
(search string)
addItem\S\S\Snbsp\S\Snbsp\SSolutions\S\S\s\Shttp\S
//server/future\Sprotoype/asp/gaPLMList\Sasp\Splm=NM-OSS\S\S\s\Stop\S\S\S
(replacement string)
addItem\(' nbsp;Solutions',\s'
but it dies becuase the second string has spaces and single quotes.
of javascript code I have in over 50 files,
but since there are single quotes in it, I am having
trouble. any suggestions would be great.
Line I'm trying to change
addItem(' Solutions', ' 'top');
to:
addItem(' Solutions', ' 'top');
Code:
#!c:\perl
#
# change all occurances of a string in a file to another string
#
if ($#ARGV != 1) {
print "usage: TextChanger oldstring newstring\n";
exit;
}
#$file = $ARGV[0];
$old = $ARGV[0];
$new = $ARGV[1];
$tempfile = "temp.000";
opendir(DI, ".");
@files = readdir(DI);
$i = 0;
while($i <= $#files){
$file = $files[$i];
print "reading $file\n";
open(F, $file);
open(TF, ">$tempfile");
# read in each line of the file
while ($line = <F>) {
$line =~ s/$old/$new/g;
print TF $line;
close(F);
close(TF);
open(F, ">$file");
open(TF, "$tempfile");
# write each line back to the original file
while ($line = <TF>) {
print F $line;
}
close(F);
close(TF);
unlink($tempfile);
$i++
}
print "changed $i files\n";
I'm envoking the code this way:
C:\test>perl textchanger.perl
(search string)
addItem\S\S\Snbsp\S\Snbsp\SSolutions\S\S\s\Shttp\S
//server/future\Sprotoype/asp/gaPLMList\Sasp\Splm=NM-OSS\S\S\s\Stop\S\S\S
(replacement string)
addItem\(' nbsp;Solutions',\s'
but it dies becuase the second string has spaces and single quotes.