Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String replacement with spaces and single quotes

Status
Not open for further replies.

glwong

Programmer
Jul 13, 2001
15
US
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');

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;nbsp;Solutions',\s'

but it dies becuase the second string has spaces and single quotes.
 
Why not just invoke it with:
(search string)
gaPLMList.asp?plm=NM-OSS
(replace string)
SolutionList.asp?plm

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
unfortunately the "gaPLMList.asp" part of the string is in
the search files multiple times, the discriminating
part of the string is in the begining. "addItem\('&nbsp;nbsp;Solutions'". Sorry I needed to elaborate more.
 
This might be a bit obtuse, but could you change the single quotes to for example zZz, then run your original replacement, and finally sub zZz with single quotes.

If this makes any sense

Just a thought
--Paul
 
I didn't try much to make this work from the command line - the pattern you're using looks too complex and needs far too many characters escaped to really be practical for use on the command line. If you don't mind hard coding the substitution, the following code appears to work (you could always comment it out after you're done.) The sections of the substitution are broken up a bit for legibility/clarity:

Code:
my $text = "addItem('&nbsp;&nbsp;Solutions', '[URL unfurl="true"]http://server/future-protoype/asp/gaPLMList.asp?plm=NM-OSS',[/URL] 'top')";

$text =~ s<
	(\QaddItem('&nbsp;&nbsp;Solutions'\E.*)
	\QgaPLMList.asp?plm=NM-OSS\E(.*)>
	<$1SolutionList.asp?plm$2>x;
print "$text\n"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top