Search and replace question

Discussion in 'Perl Misc' started by laredotornado, Jul 29, 2011.

  1. 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
     
    laredotornado, Jul 29, 2011
    #1
    1. Advertising

  2. laredotornado

    Uri Guttman Guest

    >>>>> "l" == laredotornado <> 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 -------
     
    Uri Guttman, Jul 29, 2011
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Jul 30, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McKay
    Replies:
    3
    Views:
    1,325
    Thomas Weidenfeller
    Jan 21, 2004
  2. Abby Lee
    Replies:
    5
    Views:
    443
    Abby Lee
    Aug 2, 2004
  3. jac
    Replies:
    7
    Views:
    138
    David Squire
    May 24, 2006
  4. sivga
    Replies:
    2
    Views:
    111
    John W. Krahn
    Nov 7, 2007
  5. Replies:
    1
    Views:
    521
    Rainer Weikusat
    Jun 21, 2012
Loading...

Share This Page