LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-07-2005, 12:36 PM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
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!
 
Old 03-07-2005, 01:29 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 03-07-2005, 01:59 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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.
 
Old 03-07-2005, 03:14 PM   #4
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
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.
 
Old 03-07-2005, 03:17 PM   #5
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
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...
 
Old 03-07-2005, 06:48 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
#!/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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
inserting data in mysql zuessh Linux - Software 4 05-19-2003 01:00 PM
MySQL - Inserting Data Not Happening! Rhapsodic Programming 4 10-26-2002 10:58 PM
perl - writing a data structure to a file.. is it possible? paavaka Programming 4 07-14-2001 12:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration