Gossamer Forum
Home : General : Perl Programming :

mid-html file appending

Quote Reply
mid-html file appending
Hi,

I'm using some basic perl to write out a text file, containing sequential lines of information:

log.txt:

info1: data 1: blah; data 2: blah
info2: data 1: blah; data 2: blah
info3: data 1: blah; data 2: blah
etc
etc

I'm then using a shtml 'wrapper' to display the data in a browser:

log.shtml:

<html>
<head>
<title>
log
</title>
</head>
<body>
<small>
<!--#include file="log.txt" -->
</small>
</body>
</html>

I'm using Dan Steinman's site search script at
http://www.dansteinman.com/
to index & provide a search tool for the entire site, including the log file.

Problem is, it doesn't seem to want to index either any .txt files, or follow anything produced via an
<!--#include--> directive.

So, i'd like to do away with the
perl -> .txt file -> include .txt file in .shtml file
method, and just edit a plain .html file, preserving the ending tags whilst adding lines of data just before </body></html>.

What I can't figure out, is how to use perl to insert a line of data not totally at the end of the .html file, but rather, before the ending </body></html> tags.

If I just use >> in perl to just do an 'append' each time data comes along, I get:

.
.
.
data
</body></html> (original file)
data
</body></html> (result of first data write)
data
</body></html> (result of 2nd data write)
etc

Should I try & parse the whole existing file, write to a temporary file, and overwrite back to the original file, adding in the new line of data in the process, or is there a better way of 'inserting' data lines? Either way, I'd really appreciate any tips on how to do it...

Sorry to ramble but my head hurts and none of my beginners' books are helping much!Unsure

Cheers, Neil
Quote Reply
Re: [ten_pair] mid-html file appending In reply to
Completly untested, and my Perl is a little rusty, but how about;

Code:
#!/usr/bin/perl

open(DATA, "data.txt") || die "Cant open file. Reason: $!";
$data = <DATA>;
close(DATA);

open(FILE, "file.html") || die "Cant open file. Reason: $!";
@data = <FILE>;
close(FILE);

foreach (@data) {

if ($_ =~ /<\/small>/) { $put_back .= $data . $_; }
else { $put_back .= $_; }

}

open(FILE, ">>file.html") || die "Cant open file. Reason: $!";
print FILE $put_back;
close(FILE);

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [ten_pair] mid-html file appending In reply to
Yeah, I think I get the idea.. Appreciate your suggestion, Andy, I'll have a play with that, thanks!
Smile
Quote Reply
Re: [ten_pair] mid-html file appending In reply to
You could chop about 15 lines off Andy's code using seek.

Code:
my $file = '/path/to/file';

open FH, ">>$file" or die $!;
seek FH, -14, 0; # -14 sets the current position to just before </body></html>
print FH "Something";
close FH;

That's all you need :)
Quote Reply
Re: [Paul] mid-html file appending In reply to
How did you get that 14?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] mid-html file appending In reply to
Count the characters:

</body></html>

- wil
Quote Reply
Re: [Wil] mid-html file appending In reply to
Riiigh, but how does it know to goto that line? Hehas '0' specified?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] mid-html file appending In reply to
http://www.perldoc.com/...1/pod/func/seek.html

It's there to be read.

Last edited by:

Paul: Jul 11, 2002, 6:59 AM
Quote Reply
Re: [Paul] mid-html file appending In reply to
Wink Thanks for the hints guys, it's all working great now. Appreciate your info.
Quote Reply
Re: [Paul] mid-html file appending In reply to
lol