Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Perl Regex One-Liner To Substitute Multiline Text

Posted by w3p706 (w3p706), 11 January 2005
Hello

I'd like to ask this question in the other way: Why does this work?

I'did the following:

Code:
perl -pi.bak -e "s/^CREATE(.*)$AS/CREATE$1WITH ENCRYTION \n/msg" FILE.TXT


This line peplaces

Code:
CREATE  VIEW ViewXYZ
AS


with

Code:
CREATE  VIEW ViewXYZ
WITH ENCRYTION
AS



As i understand the regex, the 'AS' shouldn't be in the subtituted text, because in the second part of the regex (the replacement) 'AS' is missing.

First i tried with something like this:

Code:
s/CREATE(\s+\S+\s+\S+\s+)AS/CREATE$1WITH ENCRYTION\nAS/msg


Why this isn't working too?

This isn't the first time i run into trouble with multiline replacement, but every time i found a way to do it. But finally i like to know if there is a general error in my imagination of regex in multiline scenarios.


Thank You Very Much For Your Suggestions!

BTW: I'm Working with Active State Perl v5.8.2 on W2k, and Perl is acting the same if files are fomatted with unix or windows linebreaks.


Posted by Custard (Custard), 11 January 2005
Hi,

I don't have time to test right now, but from your first regex, it looks like the regex is stopping at the $ so the AS gets left out of the substitution.

And you may find some mileage in the 'm' modifier on the sub ( s///m ). This specifies that $ can match end of line within a string.
(Sorry, just noticed you are using 'm' already!!!)

Also you will probably have to unset $/ to make perl ignore the line breaks.

This may not be what you want to do though. Maybe your file is big ?

I'll have a read up and get back to you on this later.

B

Posted by admin (Graham Ellis), 11 January 2005
I'm noticing that the original post was a one-liner with the Perl command on the command line and embedded in double quotes.  Now - I'm not a Windows expert, but on Linux and Unix that would cause a problem as the $AS would be treated as a shell variable.

Posted by w3p706 (w3p706), 11 January 2005
I believe this is not the case here, because in windows the % sind indicates a env-var.

And in this man we have it too:
http://aspn.activestate.com/ASPN/docs/ActivePerl/lib/Pod/perlwin32.html#usage_hints_for_perl_on_win32
See the last paragraph of 'Using perl from the command line'

But i've seen that if i use -w then the following warnig is generated:

Code:
Name "main::AS" used only once: possible typo at -e line 1.

So Perl threat this as var, witch explains the Error.


I tryed to unset the $/ and it worked! THANK YOU!

Now I'll stick with:

Code:
perl -w -pi.bak -e "undef $/; s/(CREATE\s+VIEW\s+\S+\s*)AS/$1WITH ENCRYPTION\nAS/msgi"


Thats the magic of Perl......

Posted by Custard (Custard), 12 January 2005
Just thought of something else looking at your original one-liner.  AFAICR  m// will interpolate variable names, so the $AS would look like a variable and would produce an error under 'use strict'.

B



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho