Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > in place edit.

Reply
Thread Tools

in place edit.

 
 
Nene
Guest
Posts: n/a
 
      11-02-2009
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?
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      11-02-2009
On Mon, 2 Nov 2009 06:39:21 -0800 (PST), Nene <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removal from host of a Wireless network place (other computer) =?Utf-8?B?c3N0YW5qYg==?= Wireless Networking 2 04-08-2005 02:52 PM
can you use a wireless repeater/access point in place of a WiFi card? David Leslie Wireless Networking 2 01-16-2005 03:54 PM
Can I run two networks in the same place Oliver Gunnell Wireless Networking 2 12-17-2004 04:49 AM
Type mismatch using Mozilla ActiveX in place of Microsoft WebBrowser? Noozer Firefox 0 05-19-2004 08:08 AM
Re: How to store the profile in a place different from C:\Documentsand howdeedoo@utter.pla.net Firefox 0 04-23-2004 10:02 PM



Advertisments