Link to home
Start Free TrialLog in
Avatar of 1stomni
1stomni

asked on

how to escape a quote

In a script that does a search and replace in multiple documents I am doing the following.

$search=""MEDHOD="POST"";
$replace=""METHOD="GET"";

The problem is that the quotes that are in the string that I am searching for conflict with the quotes that surround the entire string in perl.  

How do I make it so this works.

Thanks

ASKER CERTIFIED SOLUTION
Avatar of olthoff
olthoff

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jhurst
jhurst

$search="\"METHOD=\"POST\"";

However, I think you are still one set short since you will note that there are quotes starting at METHOD and ending after the =
Avatar of 1stomni

ASKER

This worked :) thanks....

$search='"MEDHOD="POST"';
$replace='"METHOD="GET"';

However I need one more thing.....

When I tried to do this it can't find it when the question mark is in there...  I have tried with and without the \.  If I change the string to only include up to php3 it works, only when I include the question mark it won't find it... please help :)  I included the script below.

$search="http://www.1888wwsports.com/phpshop/cart.php3\?action=add";

$replace="http://www.1888wwsports.com/phpshop/cart.php3";


#!/bin/perl
#
# Kevin Kadow (kadokev@msg.net) <A HREF="http://www.msg.net/kadow/">http://www.msg.net/kadow/</A>
# Can be downloaded from
#<A HREF="http://www.msg.net/utility/small/search.html">http://www.msg.net/utility/small/search.html</A>
# For 1stomni, free for all to use as long as the comments are kept intact!
#
# Search and replace strings in html files
#
require 'find.pl';

#
# Change these variables as needed.
#
$dir="/export/home/worldwide/htdocs/clothingtest";

$search="http://www.1888wwsports.com/phpshop/cart.php3\?action=add";

$replace="http://www.1888wwsports.com/phpshop/cart.php3";

warn <<"EOF";
Starting search/replace script $0
  Search all files below $dir
  Looking for string $search
  Replace the string with $replace

EOF


#
# Do the actual work
#
&find($dir);
warn "Scanned $files making $changes changes.\n";
exit(0);

################ SUBROUTINES FOLLOW #################################

#wanted
#
# This routine is called by 'find', once for each file, divectory, etc.
#
sub wanted {

#Skip everything except files named .html or .shtml
#return unless( -f $_ && m/\.[s]*html$/);
#Skip everything except files named .html or .shtml
return unless( -f $_ && m/\.s?HTML?$/i);

&update("$dir/$_");
}


#update
#
# Do the search-and-replace on a given file
#
sub update {
local($file)=@_;

local(@contents);

# Open the file for reading and writing (the + does that)
unless(open(FILE,"+<$file")) {
      warn "Cannot open $file for updating, Error: $!\n";
      return;
      }

warn "Updating $file\n";
$files++;

# Read the contents into an array, replacing the string as we go
while ($_=<FILE>) {
      $changes += s/$search/$replace/ig;
      push(@contents,$_);
      }

# Go to the beginning of the file
seek(FILE,0,0);

# Update the contents with the data from our array
print FILE (@contents);
close(FILE);
}

###EOF###

I have the same problem and haven't figured out.  I use index and substr as a replacement.

$x = index($Search, $Replace);
if ($x > -1) {
  $_ = substr($_, 0 $x) . $Replace . substr($_, $x+length($Search)+1);
};