0

I am trying to use perl to search and replace some outdated MySQL commands

perl -i -pe 'BEGIN{undef $/;} s/if @RequestID.*@NextValue=@TableNameID output/if @RequestID is not NULL\n                begin\n                        select @TableNameID = @TableNameID + 1/smgi' $file1

However currently the file remains exactly the same before and after implying the regex isn't matching?

I have the flags smgi turned on so .* also matches new lines taking advice from Multiline search replace with Perl

Here is a snip of the file I am trying to match

        if ( @@rowcount = 0 )
                return 1

        update Sequence set CurrentValue = @TableNameID where Code = 'TableName'
end

/* TableNames - Approval Request ID */
if @RequestID is not NULL
begin
        exec spGetNextSequence @Name='TableName', @NextValue=@TableNameID output

        insert into TableName        /* RequestID */
        (
                TableNameID,
                TxnVrsnID,
                FldNmCod,

If you test the pattern at http://regexpal.com/ (or any similar regex tester) - with smi on it matches fine?

Pattern: if @ApprovalRequestID.*@NextValue=@TxnDecoratorID output


This is the perl slightly split up so you can see what's going on

perl -i -pe 'BEGIN{undef $/;} s/
if @RequestID.*@NextValue=@TableNameID output/
if @RequestID is not NULL\n
                begin\n
                        select @TableNameID = @TableNameID + 1
/smgi' $file1
3
  • This is why you use -w even with one-liners. Commented Sep 5, 2012 at 14:55
  • What does -w do? Edit: It produces warnings Commented Sep 5, 2012 at 15:01
  • 1
    It does indeed. In this case, it would have told you the problem right away: Possible unintended interpolation of @RequestID in string at -e line 1. Commented Sep 5, 2012 at 19:41

1 Answer 1

4

@name is interpolated as a named Perl array inside of regular expression patterns. Escape the @ characters:

perl -i -pe 'BEGIN{undef $/;} s/
if \@RequestID.*\@NextValue=\@TableNameID output/
if \@RequestID is not NULL\n
                begin\n
                        select \@TableNameID = \@TableNameID + 1
/smgi' $file1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.