LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: Inserting new data into the middle of a file (https://www.linuxquestions.org/questions/programming-9/perl-inserting-new-data-into-the-middle-of-a-file-298771/)

R00ts 03-07-2005 12:36 PM

Perl: Inserting new data into the middle of a file
 
I've googled for an answer to this and I'm not finding a whole lot of help. One page said that there is no native mechanism for this in Perl and you have to perform it by creating temporary files and such.


Specifically I'm working with HTML files generated by Perl to report data and I need to check if a file exists and if it does, look for the final two tags (</body> and </html>) and insert new data before those two lines. My O'Reily Perl book is still being shipped to me so I can't refer to that either. Can anyone help me out? It seems like it should be something quick and simple... Thanks! :)

aluser 03-07-2005 01:29 PM

How large are these files? The easiest way to do this is to just read the whole file into a string or array, make the changes, and then overwrite the entire with your new version. There are trickier and more efficient ways to do it particularly in a case like yours where you are just changing something near the end of the file, but it'd only be worth it on a really high traffic site, or on really huge (megabytes) files.

keefaz 03-07-2005 01:59 PM

If your file are not so much large, say < 200k, you could insert your text as :
Code:

#!/usr/bin/perl -i.orig -p

print "this is the text you want to insert\n" if /<\/body>/;

..and invoke the script with ./script.pl file.html
This will backup original file.html in file.html.orig before do the change
(replace -i.orig with -i at first line if you don't want this feature)

R00ts 03-07-2005 03:14 PM

The files are definitely all under 200K. They are just HTML tables reporting some data.

Code:

#!/usr/bin/perl -i.orig -p

print "this is the text you want to insert\n" if /<\/body>/;


How exactly does the -i.orig -p work? I don't think it will work corerctly for me in my case because I already have a somewhat complex command argument parser. I have a bunch of single letter (-x) options, a '-g=output_file.html' for specifying what HTML file to create or append to, and all the other arguments are various text input files. My script first runs through each file for identification information to discern what type of file it is, then calls the appropriate parser code to extract the key information I want, and repeats it for each file until I have an array of generated information.

The HTML is generated at the very end and the only way you can specify it to generate HTML and tell ti what file to print ot is with that -g option. I'd rather not read a file into a string and then manipulate the string because I have a lot of print statements that go into the output file and I don't want to have to duplicate the same code over for the string case. Thanks for the help guys. :)

R00ts 03-07-2005 03:17 PM

Just thought of something. Can I remove the </body> and </html> lines from the file, then write the new data, and then write those </body> and </html> tags back at the end of the file before I close it? I think that would be a good workaround. I haven't done a lot of file manipulation with Perl though (in fact I haven't done much of anything with Perl. :rolleyes: ) I will google and see if I can find this info in the meantime...

keefaz 03-07-2005 06:48 PM

#!/usr/bin/perl -i.orig -p could be replaced with:
Code:

$file = "/path/to/file.html";
rename $file, "$file.orig";
open FILE, ">", $file;
open ORIG, "<",  "$file.orig";
while (<ORIG>) {
    print FILE "text to insert\n" if /<\/body>/;
    print FILE $_;
}
close ORIG;
close FILE;

I don't know the flow of your whole script though, you may
have more success with your idea (remove the </body>
and </html> lines) so you could append your text to the
file instead of insert it


All times are GMT -5. The time now is 08:32 PM.