Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   in place edit. (http://www.velocityreviews.com/forums/t911703-in-place-edit.html)

Nene 11-02-2009 02:39 PM

in place edit.
 
I used the same code below (provided by Randy) to do in place edits on
a config file and it works great, but I when I try to use the same
code for an html file, it does not work.

###Below is the code###

#!/usr/bin/perl -w
use strict;
use warnings;
my $stuff = "</tr>";

my $a = "insert_text_here\n";
{
local @ARGV = "misc.txt";
local $^I = ".bak"; # appended to the backup copy
while (<>) {
if ((/$stuff/..!/$stuff/) =~ /E/) { # if we're at the end of
the Listens
$_ = $a . $_; # prepend the line to the next line
}
print; # but print whatever we have
}
}




###Below is the result of the script when I run it#



<tr>
<td>http://xxx.xxx.xxx.xxx</td>
<td>
<a href='https://xxx:8002'>
https://xxx.xxx.xxx.xxx:8002
</a>
</td>
</tr>
insert_text_here
<tr>
<td>http://xxx.xxx.xxx.xxx</td>
<td>
<a href='https://xxx.xxx:8010'>
https://xxx.xxx.xxx:8010
</a>
</td>
</tr>
insert_text_here
<tr>
<td>http://xxx.xxx.xxx.xxx:8145</td>
<td>
<a href='https://xxx.xxx.xxx:8148'>
https://xxx.xxx.xxx:8148
</a>
</td>
</tr>
insert_text_here


Text in this line
Text in this line
Text in this line
####

As you can see "insert_text_here" is appended at the bottom of every
"</tr>"; I only want it appended to the last "</tr>".
If this is not possible, is there a module that will read a specific
number of lines backwards and then insert?

sln@netherlands.com 11-02-2009 04:08 PM

Re: in place edit.
 
On Mon, 2 Nov 2009 06:39:21 -0800 (PST), Nene <rodbass63@gmail.com> wrote:

>I used the same code below (provided by Randy) to do in place edits on
>a config file and it works great, but I when I try to use the same
>code for an html file, it does not work.
>
>###Below is the code###
>
>###Below is the result of the script when I run it#
>
>As you can see "insert_text_here" is appended at the bottom of every
>"</tr>"; I only want it appended to the last "</tr>".
>If this is not possible, is there a module that will read a specific
>number of lines backwards and then insert?


Something like this.. where you adapt it to create a new file.

use strict;
use warnings;

my $line_to_insert = "insert\n";
my $last;
open DATA, '<data.txt' or die "can't open data.txt: $!";

{
while (<DATA>) {
$last = tell(DATA) if /<\/tr>/;
}
if (defined $last) {
seek DATA,0,0;
while (<DATA>) {
print;
print $line_to_insert if tell(DATA) == $last;
}
}
}
close DATA;

__END__

-sln


All times are GMT. The time now is 09:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.