in place edit.

N

Nene

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?
 
S

sln

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top