Replacing text containing parenthesis

Discussion in 'Perl Misc' started by Mark Hobley, Apr 17, 2009.

  1. Mark Hobley

    Mark Hobley Guest

    I am using perl as a find and replace tool. I want to replace the following
    text with a null:

    $(srcdir)/config/override.m4

    I tried the following:

    find ./ -name Makefile.in -exec perl -pi -e \
    "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;

    Unfortunately, this gives an error:

    Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
    config/override\.m4/ at -e line 1, <> line 1.

    I guess that the brackets are being evaluated first, and being treated as part
    of the expression.

    How do I represent literal brackets within the expression?

    Mark.

    --
    Mark Hobley
    Linux User: #370818 http://markhobley.yi.org/
     
    Mark Hobley, Apr 17, 2009
    #1
    1. Advertising

  2. (Mark Hobley) wrote in
    news::

    > I am using perl as a find and replace tool. I want to replace the
    > following text with a null:
    >
    > $(srcdir)/config/override.m4
    >
    > I tried the following:
    >
    > find ./ -name Makefile.in -exec perl -pi -e \
    > "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;
    >
    > Unfortunately, this gives an error:
    >
    > Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE
    > top_srcdir\)/\.\./ \ config/override\.m4/ at -e line 1, <> line 1.
    >
    > I guess that the brackets are being evaluated first, and being treated
    > as part of the expression.


    No. The shell gets to see the string first, replacing \$\( with $\(
    where $\ is the output record separator.

    So, replacing \$\( with \\$\( should work (untested).

    Note also that the string you gave:

    $(srcdir)/config/override.m4

    will not match the pattern you use above.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Apr 17, 2009
    #2
    1. Advertising

  3. Mark Hobley wrote:
    > I am using perl as a find and replace tool. I want to replace the following
    > text with a null:
    >
    > $(srcdir)/config/override.m4
    >
    > I tried the following:
    >
    > find ./ -name Makefile.in -exec perl -pi -e \
    > "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;
    >
    > Unfortunately, this gives an error:
    >
    > Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
    > config/override\.m4/ at -e line 1, <> line 1.


    Your string:

    "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"

    is in double quotes so it is interpolated by the shell first.

    Run:

    echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"

    from the shell to see how the shell interpolates it.


    You probably want to use single quotes instead of double quotes.




    John
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
     
    John W. Krahn, Apr 17, 2009
    #3
  4. Mark Hobley

    Mark Hobley Guest

    John W. Krahn <> wrote:

    > Run:
    >
    > echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
    > from the shell to see how the shell interpolates it.


    Hmmm, it appeared to be ok ...

    echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
    s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;

    However, you are right about those double quotes. I will try that, and see
    what happens.

    Mark.

    --
    Mark Hobley
    Linux User: #370818 http://markhobley.yi.org/
     
    Mark Hobley, Apr 17, 2009
    #4
  5. Mark Hobley

    Mark Hobley Guest

    Ben Morrow <> wrote:

    > Using single quotes for the shell will remove one level of
    > interpolation, and make things simpler. Using alternative delimiters for
    > the s/// will help get rid of some of those backwacks, and using \Q will
    > get rid of most of the rest:
    >
    > ... -e 's!\Q\$(top_srcdir)/../config/override.m4!!g;' \;


    That is interesting. What is the capital Q?

    Mark.

    --
    Mark Hobley
    Linux User: #370818 http://markhobley.yi.org/
     
    Mark Hobley, Apr 17, 2009
    #5
  6. Mark Hobley wrote:
    > John W. Krahn <> wrote:
    >
    >> Run:
    >>
    >> echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
    >> from the shell to see how the shell interpolates it.

    >
    > Hmmm, it appeared to be ok ...
    >
    > echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
    > s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;


    You are missing the \ in front of the $ so perl interpolates that as the
    variable $\ and the left parenthesis is therefore not escaped generating
    the error message "Unmatched ( in regex;".


    John
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
     
    John W. Krahn, Apr 17, 2009
    #6
  7. Mark Hobley

    Guest

    On Fri, 17 Apr 2009 17:08:02 GMT, (Mark Hobley) wrote:

    >I am using perl as a find and replace tool. I want to replace the following
    >text with a null:
    >
    >$(srcdir)/config/override.m4
    >
    >I tried the following:
    >
    >find ./ -name Makefile.in -exec perl -pi -e \
    >"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;
    >
    >Unfortunately, this gives an error:
    >
    >Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
    >config/override\.m4/ at -e line 1, <> line 1.
    >
    >I guess that the brackets are being evaluated first, and being treated as part
    >of the expression.
    >
    >How do I represent literal brackets within the expression?
    >
    >Mark.


    You know what? I FIND it hard to believe you type this from a command shell.
    And I find it hard to believe you maintain a battery of batch files to shell
    to perl scripts quoted from a batch file to be shelled to Perl from a shelled
    shell.

    -sln
    (shell shocked!)
     
    , Apr 17, 2009
    #7
  8. On 2009-04-17, Mark Hobley <> wrote:

    (plz, don't get that as ranting) I've seen you on USENET for some time
    already, you're not supposed to ask such, excuse me, stupid questions.

    *SKIP*
    > find ./ -name Makefile.in -exec perl -pi -e \
    > "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;


    By an accident your I<-e> oneliner becomes

    {57028:35} [0:0]$ echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
    s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;

    > Unfortunately, this gives an error:
    >
    > Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
    > config/override\.m4/ at -e line 1, <> line 1.


    Not exactly. I<$\> is a special variable for Perl, look it in
    C<perldoc perlvar>. By default it's B<undef>ined, so evaluates to empty
    string, and since you don't C<use warnings;> (or I<-w> switch for
    oneliners) you stay unaware of this.

    Advice: get used to single quotes when writing one-liners.

    As of leaning-teeth-sticks:

    's{\$\(top_srcdir\)/../config/override\.m4}{}g'

    *CUT*

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
     
    Eric Pozharski, Apr 17, 2009
    #8
  9. (Mark Hobley) wrote in
    news::

    > John W. Krahn <> wrote:
    >
    >> Run:
    >>
    >> echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
    >> from the shell to see how the shell interpolates it.

    >
    > Hmmm, it appeared to be ok ...
    >
    > echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
    > s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;


    How is that OK?

    Now, instead of \$\( you have $\(

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Apr 21, 2009
    #9
    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. Brown Smith
    Replies:
    1
    Views:
    506
    Frankie
    Jun 25, 2005
  2. .Net Sports
    Replies:
    4
    Views:
    2,135
    Eliyahu Goldin
    Jun 29, 2005
  3. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    294
    Chris Hohmann
    Apr 11, 2005
  4. Carl Cunningham

    Regular expressions and parenthesis in match text

    Carl Cunningham, Sep 15, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    118
    Carl Cunningham
    Sep 15, 2003
  5. Replies:
    5
    Views:
    160
    RedGrittyBrick
    Sep 29, 2008
Loading...

Share This Page