Fwd: [CMI.PM] Multi-line substitution?

Arun Bhalla bhalla at uiuc.edu
Wed Jan 21 16:15:26 CST 2004


Argh.  I sent this last night, I don't know why it didn't go through.

----- Forwarded Message

To:  CU Perlmongers <champaign-urbana at pm.org>
From:  Arun Bhalla <bhalla at uiuc.edu>
Reply-To:  Arun Bhalla <bhalla at uiuc.edu>
Subject:  Re: [CMI.PM] Multi-line substitution? 
Date:  Tue, 20 Jan 2004 19:10:14 -0600


Well, the problem in your example is that you are feeding the "-p"
switch to perl.  perl is feeding one line at a time to your script.
There are two ways to go about it.  

Here is the easiest:

You can pass perl an argument (0777) which will tell perl to process 
whole files at a time instead of splitting on any particular character
(such as the default newline).  So your script will work as is if
you change the shebang line to: #!/usr/bin/perl -p0777i.old


If you wish to make it into something more than a simple one-liner,
if you read everything into a line (e.g.:)

   $document = join('', <>);

then process that (e.g.:)

   $document =~ s/<p>.*<table border="0" width="800" cellpadding="15" cellspaci
n
g="0">/s;

you should be golden.  If this pattern occurs multiple times in the same
file, you would need both a 'g' modifier and to add a '?' after the '*' to
make it less greedy.


Arun




Mitch Kutzko writes:
> Hi,  folks -- I'm trying to remove the following three lines from a website
> I run, and these three lines occur in many files on the site:
> 
> 	<p>
> 	
> 	<table border="0" width="800" cellpadding="15" cellspacing="0">
> 
> 
> I've looked in the Cookbook and on the web, and by all the accounts and
> examples I can find, this should work, but it doesn't.  Doesn't do a dang
> thing, in fact.  (Which tells me it's not matching, of course, but I don't
> see how it can not match.)
> 
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>.*<table border="0" width="800" cellpadding="15" cellspacing="0">/
> /s
> 
> If I reduce the match string down to the whole <table> command by itself,
> it works fine.  Basically, it just refuses to match across more than one
> line.  
> 
> I've tried the following, all with the same utter lack of results over
> multiple lines:
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>.*<table border="0" width="800" cellpadding="15" cellspacing="0">/
> /sm
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>.*<table border="0" width="800" cellpadding="15" cellspacing="0">/
> /smg
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>.*<table border="0" width="800" cellpadding="15" cellspacing="0">/
> /m
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>.*<table border="0" width="800" cellpadding="15" cellspacing="0">/
> /mg
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>.*<table border="0" width="800" cellpadding="15" cellspacing="0">/
> /sm
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>.*?<table border="0" width="800" cellpadding="15" cellspacing="0">
> //s
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>..<table border="0" width="800" cellpadding="15" cellspacing="0">/
> /s
> 
> 	#!/usr/bin/perl -pi.old
> 	s/<p>...<table border="0" width="800" cellpadding="15" cellspacing="0">
> //s
> 
> 	#!/usr/bin/perl -pi.old
> 	s/\<p\>.*\<table border="0" width="800" cellpadding="15" cellspacing="0
> "\>//s
> 
> etc...  (You get the idea by now, I'm sure...  ;-) )
> 
> This should be (and, of course, will turn out to be) painfully simple to
> do.  What am I not doing?
> 
> 
> Thoughts?  Ideas?  Suggestions?  Tequila?
> 
> ;-)
> 
> As always, thanks in advance!
> 
> Mitch
> --
> Mitch Kutzko | mitch at dast.nlanr.net | mitch at ncsa.uiuc.edu | 217-333-1199
> http://hobbes.ncsa.uiuc.edu/
> _______________________________________________
> Champaign-Urbana mailing list
> Champaign-Urbana at mail.pm.org
> http://mail.pm.org/mailman/listinfo/champaign-urbana
> 

--
Arun Bhalla

----- End of Forwarded Message




More information about the Champaign-Urbana mailing list