nr0mx has asked for the wisdom of the Perl Monks concerning the following question:

I have the following piece of text in a file :
thisquery=select \ table1.column1, \ table1.column2, \ table1.column3 alternate_title, \ table1.column4 \ from \ table1 \ where \
and I need to replace table1.column3 with some dummy id. I'm trying the following one-liner without any success. Any ideas ? If I slurp the file into a single-line string it works, but in multi-line mode it doesn't replace the desired string. What am I missing here ? Isn't the /s modifier supposed to do the matching across newlines ?
perl -pi -e 's/select(.*?)table1\.column3(.*?)from/select\1DUMMY_ID\2f +rom/sg' select.sql
A very confused monk!

Replies are listed 'Best First'.
Re: Problem with a multi-line regexp
by Zaxo (Archbishop) on Aug 27, 2004 at 14:19 UTC

    Your one-liner is reading the file one line at a time. See perlrun for changing $/ from a command line option. Paragraph mode (-000)looks like a good bet.

    After Compline,
    Zaxo

Re: Problem with a multi-line regexp
by davido (Cardinal) on Aug 27, 2004 at 14:16 UTC

    The /s modifier allows '.' to match newlines. However, if you're reading one line at a time, you're never giving the regexp an opportunity to deal with more than one line at a time. You're only reading "thisquery=select\\n" on the first iteration, for example. How can you expect the regexp to look ahead to lines you haven't read yet?

    Not knowing precisely how your input file is layed out, I'll just offer a tip in speculation. If your records are separated by more than one '\n' (newline), you might set your input record separator to '\n\n' and see what happens. ;)

    You can do that by putting a BEGIN{ $/ = qq/\n\n/; } at the beginning of your script, right before the s/select(.*?....... There's also a command line switch to alter the input record separator, but I always have to look it up in perlrun because I'm forgetful... leading me to just use the BEGIN{...} block trick.

    Hope this helps...


    Dave

Re: Problem with a multi-line regexp
by ysth (Canon) on Aug 27, 2004 at 14:22 UTC
    If you are using the regex first on "thisquery=select \\", then on " table1.column1, \\", then on " table1.column2, \\", etc., it will never match because it never sees both "select" and "table1.column3".

    Try:

    perl -pi -we '/select/ .. s/table1\.column3/DUMMY_ID/'
    which will do the substitution for the first table1.column3 found on or after each select line.

    Also, \1, \2, etc. are better written as $1, $2, etc. on the right side of a substitution., as you would have been told if you'd enabled warnings.