Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to file with out appending to the end

Status
Not open for further replies.

newtarge

ISP
Oct 27, 2003
21
US
If I were to write a scripts where I wanted to open my httpd.conf file and add a virtual tag, how do I tell it to write any where but the eof? Also not by line number.
 
The only way to do an in-place edit is to change bytes in the file on a one-for-one basis. One option is to work it like perl's -i cli switch and move the old file to a new filename (httpd.conf.bak), then open the old/original filename (httpd.conf) as a new file. Open the backup for reading and print line-by-line into the new file until you get to the point you want to insert text, insert, and continue to append the backup file. Delete the backup when the script finishes.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Another option is to search and replace for the spot where you want to put the tag in using regular expressions. This of course only works if you a spot to search for..
 
open(FILE,"httpd.conf"):
my $conf_file;
while(<FILE>){$conf_file.=$_;}
close(FILE);

$conf_file =~ s/(tex before your sub)/your info$1/gsmxi;
`mv httpd.conf httpd.conf.bak`;

open(OUTFILE,'>httpd.conf');
print OUTFILE $conf_file;
close(OUTFILE);

------

Optionally there are a few modules that handle this sort of thing:


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top