Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-07-2005, 12:36 PM
|
#1
|
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545
Rep:
|
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! 
|
|
|
|
03-07-2005, 01:29 PM
|
#2
|
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
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.
|
|
|
|
03-07-2005, 01:59 PM
|
#3
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
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)
Last edited by keefaz; 03-07-2005 at 02:02 PM.
|
|
|
|
03-07-2005, 03:14 PM
|
#4
|
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545
Original Poster
Rep:
|
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. 
|
|
|
|
03-07-2005, 03:17 PM
|
#5
|
|
Member
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545
Original Poster
Rep:
|
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...
|
|
|
|
03-07-2005, 06:48 PM
|
#6
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
#!/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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:23 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|