Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple line substitution

Status
Not open for further replies.

jonperks

Programmer
Nov 19, 2003
40
GB
i'm new to pearl and need to multiple line replace

<tr>
<td class=&quot;tablepaddingsub&quot;>
<a class=&quot;leftbar&quot; href=&quot; Fair</a>
</td>
</tr>

with

<tr>
<td class=&quot;table&quot;>
<a class=&quot;left&quot; href=&quot; Fair 2</a>
</td>
</tr>

thats how its laid out in several documents and all the ways of doing it i've seen are very confusing.

Any help would be appreciated.
 
I'm also somewhat new to Perl but I think that regular expressions in a loop over the document will help you match the pattern and replace the string...



&quot;Free will...is an illusion&quot;
 
Hmm somewhat short of an answer:

- Loop structure
- Match pattern to find the correct line
- Search the correct string (regex)
- Do the appropriate split function
- Replace the original string

I think some of the what more advanced users can provide you with more detailed information although keep in mind they won't build entire scripts for you (unless they're in a jolly good mood!). The best help you an get is by posting some of your own stuff so we can work with it..

- Raenius..

&quot;Free will...is an illusion&quot;
 
The problem is I have instances of each line again that i don't want to change, i only want to change the lines when they are all together like I have listed, if that makes sense, any code i' have can do one line at a time but not only if all 3 match
 
Several ways to do this really. The easiest, but most memory inefficient (doesn't matter for small file sets or one time operations) is to slurp the entire file in as a single string and then operate the regex on it. This is very easy as regex only likes strings and not crossing multiple lines.

open (IN, &quot;file&quot;);
$/ = undef; # Set program to slurp mode
$data = <IN>; # Slurp the data
$data s/<td class=&quot;tablepaddingsub&quot;><a class=&quot;leftbar&quot; href=&quot; Fair</a>/<td class=&quot;table&quot;><a class=&quot;left&quot; href=&quot; Fair2</a>/isg;

Got to run to work. Will post the rest in a bit.
 
Some of the other ways I was experimenting with don't work. So, slurp and replace is the best way. The only things that have to be added to it now are to escape the special characters (fixed in this version) and print the results to a file.

open (IN, &quot;oldfile&quot;);
$/ = undef; # Set program to slurp mode
$data = <IN>; # Slurp the data
close (IN);

$data =~ s/\s*<td class=&quot;tablepaddingsub&quot;>\s*<a class=&quot;leftbar&quot; href=&quot;http:\/\/link&quot;>Virtual\s*Fair<\/a>/<td class=&quot;table&quot;><a class=&quot;left&quot; href=&quot;http:\/\/Link2&quot;>Virtual Fair2<\/a>/isg;

open (OUT, &quot;>newfile&quot;); # create scratch file for modified results
print OUT $data;
close (OUT);
rename (&quot;oldfile&quot;,&quot;oldfile.bak&quot;); #make backup of original
rename (&quot;newfile&quot;,&quot;oldfile&quot;); #rename modified to original
unlink (&quot;newfile&quot;); #delete the scratch file
 
I'm getting tonnes of error messages like this

Bareword found where operator expected at test.pl line 13, near &quot;unlink (&quot;newfile&quot;
(Do you need to predeclare unlink?)
String found where operator expected at test.pl line 13, at end of line
(Missing semicolon on previous line?)
Can't find string terminator '&quot;' anywhere before EOF at test.pl line 13.
 
Hmmm :) I would have a look at line 13 if I were you.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Sorry, you actually don't need the unlink command. The scratch file gets renamed and does not leave a copy behind. Just delete that line.
 
Same error messages still, its not just line 13, its pretty much every line of my code, thats just a small bit of the errors coming up. What you've done is a different way of what I had done but i was getting the same kind of problems.

any ideas anyone?
 
post your entire script please. don't change or leave anything out.
 
It,s all sorted now I went back to the drawing board and there a few differences from your code to the final working code, mainly .* in the match string where the new line appears, the whole html code also had to be done like


'html here'


thanks for your all your help anyway, never thought i'd miss web developing so much [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top