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!

Insert new Lines at a certain point in a file 2

Status
Not open for further replies.

webstony

Technical User
Jan 29, 2001
19
DE
Hi everybody,

I have a problem concerning Filehandles. Is it possible to open a file just to insert new lines at a certain point in that file? I don't want to append the new content, I just want to write it before a specific keyword in that file.
Or do I have to read out the whole file ?? I just thought, that there must be a more elegant solution for that problem ...

Thanks in Advance,

webstony
 
If you know the exact spot in the file that you want to insert the text, and it is the same spot everytime, just load the file into an array and find the array element that corresponds to where you want to insert the file. Then insert what you want to add and push the other array element values up by 1.

Hope this helps,

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
You might be able to use seek, but, I expect that would be more work than Vic's suggestion. In order to use seek, you need to know the number of bytes to where you want to go in the file. Unless the file is enormous, Vic's approach will work very well. Alternatively, you could use a pattern replacement to replace the key word pre-pended with the string you want added in.....

#!perl
open(IPF,<&quot;file_name&quot;) or die &quot;Failed to open input file, $!\n&quot;;
while (<IPF>) { $buffer .= $_; }
close IPF;

$some_key_word = 'key_word';

# Find the key word and replace it with the prepended string
# followed by the key word. The 'e' at the end of the replacement
# causes the right side to be evaluated prior to the replace action.
# That calls the sub which constructs the replacement string
# and returns it to the s///egis statement.

$buffer =~ s/$some_key_word/&amp;build_replacement($&amp;)/es;

open(OPF,&quot;>outputFile&quot;) or die;
print OPF, $buffer;
close OPF;

sub build_replacement
{
my $key_word = shift;
$new_string = 'your new text in front of '.$key_word;
return($new_string);
}


The usual disclaimer -- I have not run this, but, it should be close.
'hope this helps....;-)


keep the rudder amid ship and beware the odd typo
 
One possibility: If the images' locations are written in relative instead of absolute directory listings, you'll have to strip the prefix directory out of the original url and add that onto it. you'll also have to check first to see whether it's an absolute or relative listing. not too much trouble, though.

&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top