Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Search and replace question (http://www.velocityreviews.com/forums/t915151-search-and-replace-question.html)

laredotornado 07-29-2011 01:45 PM

Search and replace question
 
Hi,

I'm using Perl 5.10.6 on Mac 10.6.6. I want to execute a simple
search and replace against a file ...

my $searchAndReplaceCmd = "perl -pi -e 's/\\Q${localTestDir}\\E//g' $
{testSuiteFile}";
system( $searchAndReplaceCmd );

but the problem above is the variable $localTestDir contains directory
separators, and this screws up the regular expression ...

Bareword found where operator expected at -e line 1, near "s/\Q/home/
selenium"
Backslash found where operator expected at -e line 1, near "Live\"
syntax error at -e line 1, near "s/\Q/home/selenium"
Search pattern not terminated at -e line 1.

How do I do a search and replace when the variable in questions
contains regular expression characters? Thanks, - Dave

Uri Guttman 07-29-2011 04:40 PM

Re: Search and replace question
 
>>>>> "l" == laredotornado <laredotornado@zipmail.com> writes:

l> I'm using Perl 5.10.6 on Mac 10.6.6. I want to execute a simple
l> search and replace against a file ...

l> my $searchAndReplaceCmd = "perl -pi -e 's/\\Q${localTestDir}\\E//g' $
l> {testSuiteFile}";
l> system( $searchAndReplaceCmd );

why are you calling out to a perl subprocess when you are already inside
perl? you can just read in the file, edit it and write it out again. the
-pi options aren't so useful when you just need to do that inside
perl. it is obviously causing you problems due to shell interpretation
and such. it also means your regex is more cluttered than it needed to
be.

l> How do I do a search and replace when the variable in questions
l> contains regular expression characters? Thanks, - Dave

well, you can use perl to read/edit/write the file directly. you can use
File::Slurp to make that even easier. then again, you can use
File::Slurp's edit_file_lines sub to do it even more easily.

use File::Slurp qw( edit_file_lines ) ;

edit_file_lines { s/$localTestDir//g } $test_file ;

and since you are doing the s/// globally, you can speed it up with
edit_file which slurps in the whole file into $_:

use File::Slurp qw( edit_file ) ;

edit_file { s/$localTestDir//g } $test_file ;


done. no need to escape anything or worry about shell stuff or several
other issues you had with your code.

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------

John W. Krahn 07-30-2011 11:23 AM

Re: Search and replace question
 
laredotornado wrote:
> Hi,
>
> I'm using Perl 5.10.6 on Mac 10.6.6. I want to execute a simple
> search and replace against a file ...
>
> my $searchAndReplaceCmd = "perl -pi -e 's/\\Q${localTestDir}\\E//g' $
> {testSuiteFile}";
> system( $searchAndReplaceCmd );
>
> but the problem above is the variable $localTestDir contains directory
> separators, and this screws up the regular expression ...


Ooh, how to run perl code from inside perl? I think I know that one:

{ local ( $^I, @ARGV ) = ( '', $testSuiteFile );
while ( <> ) {
s/\Q$localTestDir\E//g;
print;
}
}



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein


All times are GMT. The time now is 10:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.