Problem: perl negative look-ahead assertion in multi-line mode

Discussion in 'Perl Misc' started by cibalo, May 22, 2013.

  1. cibalo

    cibalo Guest

    Hello,
    I have updated/modified my mysql database in my free website recently.
    I have to re-code all my php files such that an extra statement has to
    be inserted before the last mysql_query call. It may have more than
    one mysql_query call in some of my php files.

    I try to use a negative look-ahead assertion to make such a change as
    follows.

    I would like to insert just one "new_insert" line before the "coding
    line 4". And my php file looks like this.

    $ echo -e "coding line 1 test 1\ncoding line 2 mysql_query 2\ncoding
    line 3 test 3\ncoding line 4 mysql_query 4\ncoding line 5 test 5"
    coding line 1 test 1
    coding line 2 mysql_query 2
    coding line 3 test 3
    coding line 4 mysql_query 4
    coding line 5 test 5

    Then I perl-regex with negative look-ahead assertion in multi-line
    mode (/smg or /sg or /mg) like this:

    $ echo -e "coding line 1 test 1\ncoding line 2 mysql_query 2\ncoding
    line 3 test 3\ncoding line 4 mysql_query 4\ncoding line 5 test 5" |
    perl -pe '/mysql_query(?!.*mysql_query)/smg && print "new_insert\n"'
    coding line 1 test 1
    new_insert
    coding line 2 mysql_query 2
    coding line 3 test 3
    new_insert
    coding line 4 mysql_query 4
    coding line 5 test 5

    However, my "coding line 2" gets changed too. And that is not what I'm
    looking for.

    Or, perl negative look-ahead assertion doesn't like me! Can you please
    let me know what I'm missing?

    Thank you very much in advance!!!

    Best Regards,
    cibalo
     
    cibalo, May 22, 2013
    #1
    1. Advertising

  2. On 5/22/2013 2:51 AM, cibalo wrote:
    > Hello,
    > I have updated/modified my mysql database in my free website recently.
    > I have to re-code all my php files such that an extra statement has to
    > be inserted before the last mysql_query call. It may have more than
    > one mysql_query call in some of my php files.
    >
    > I try to use a negative look-ahead assertion to make such a change as
    > follows.
    >
    > I would like to insert just one "new_insert" line before the "coding
    > line 4". And my php file looks like this.
    >
    > $ echo -e "coding line 1 test 1\ncoding line 2 mysql_query 2\ncoding
    > line 3 test 3\ncoding line 4 mysql_query 4\ncoding line 5 test 5"
    > coding line 1 test 1
    > coding line 2 mysql_query 2
    > coding line 3 test 3
    > coding line 4 mysql_query 4
    > coding line 5 test 5
    >
    > Then I perl-regex with negative look-ahead assertion in multi-line
    > mode (/smg or /sg or /mg) like this:
    >
    > $ echo -e "coding line 1 test 1\ncoding line 2 mysql_query 2\ncoding
    > line 3 test 3\ncoding line 4 mysql_query 4\ncoding line 5 test 5" |
    > perl -pe '/mysql_query(?!.*mysql_query)/smg && print "new_insert\n"'
    > coding line 1 test 1
    > new_insert
    > coding line 2 mysql_query 2
    > coding line 3 test 3
    > new_insert
    > coding line 4 mysql_query 4
    > coding line 5 test 5
    >
    > However, my "coding line 2" gets changed too. And that is not what I'm
    > looking for.
    >
    > Or, perl negative look-ahead assertion doesn't like me! Can you please
    > let me know what I'm missing?


    Since you want to insert just before the last mysql_query line, you can
    use .* greediness to avoid a negative look-ahead:

    echo ... |
    perl -0777 -pe 's/ (.*) (coding .*? query .*? \n)/$1new_insert\n$2/sx'


    --
    Charles DeRykus
     
    Charles DeRykus, May 22, 2013
    #2
    1. Advertising

  3. cibalo <> writes:
    > I have updated/modified my mysql database in my free website recently.
    > I have to re-code all my php files such that an extra statement has to
    > be inserted before the last mysql_query call. It may have more than
    > one mysql_query call in some of my php files.
    >
    > I try to use a negative look-ahead assertion to make such a change as
    > follows.
    >
    > I would like to insert just one "new_insert" line before the "coding
    > line 4". And my php file looks like this.
    >
    > $ echo -e "coding line 1 test 1\ncoding line 2 mysql_query 2\ncoding
    > line 3 test 3\ncoding line 4 mysql_query 4\ncoding line 5 test 5"
    > coding line 1 test 1
    > coding line 2 mysql_query 2
    > coding line 3 test 3
    > coding line 4 mysql_query 4
    > coding line 5 test 5


    Strictly, this isn't related to the shell and not related to Perl, but
    if you want to make a modification to a set of text files, have you
    considered using a text editor?

    NB: The 'compound command' below was executed in a directory which
    contained three copies of the 'demo file' whose content was included in
    the original posting.

    for x in *; do
    ed "$x" <<'TT'
    $
    ?mysql_query?i
    new_insert
    ..
    wq
    TT
    done

    This invokes ed on every file in the current directory, with 'command
    input' coming from a here document (separator quoted to prevent 'shell
    expansion' of the text). The ed commands are

    $

    Go to the last line of the input file.

    ?mysql_query?i

    Search backwards (regexp) for the first msql_query and insert text in
    front of that line.

    new_insert

    The text to insert.

    ..

    'Exit insert-mode' command.

    wq

    Write changes and quit (non-standard, BSD-originated command).
     
    Rainer Weikusat, May 23, 2013
    #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. Simon Fairey

    Negative Look Ahead question?

    Simon Fairey, Jun 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    157
  2. Jason Friedman

    Re: Regular expression negative look-ahead

    Jason Friedman, Jul 2, 2013, in forum: Python
    Replies:
    0
    Views:
    95
    Jason Friedman
    Jul 2, 2013
  3. Ian Kelly
    Replies:
    0
    Views:
    107
    Ian Kelly
    Jul 2, 2013
  4. Jason Friedman

    Regular expression negative look-ahead

    Jason Friedman, Jul 2, 2013, in forum: Python
    Replies:
    1
    Views:
    78
    Neil Cerutti
    Jul 2, 2013
  5. Jason Friedman

    Re: Regular expression negative look-ahead

    Jason Friedman, Jul 4, 2013, in forum: Python
    Replies:
    0
    Views:
    86
    Jason Friedman
    Jul 4, 2013
Loading...

Share This Page