Perl: Inserting new data into the middle of a file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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.
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)
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.
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. ) I will google and see if I can find this info in the meantime...
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.