How to insert a line into midle of the existing file

consider an example where i want to insert few lines of text when particular patter matches(if $line=~m/few lines in here/ then insert lines in next line):

*current file:*

"This is my file and i wanna insert few lines in here  and other
text of the file will continue."

*After insertion:*

"This is my file and i wanna insert few lines in here  this is my
new text which i wanted to insert and other text of the file will
continue."

This is my code:

my $sourcename = $ARGV[1];
my $destname = $ARGV[0];
print $sourcename,"\n";
print $destname,"\n";
my $source_excel = new Spreadsheet::ParseExcel;  
my $source_book = $source_excel->Parse($sourcename) or die "Could not open source Excel file $sourcename: $!";

my $source_cell;
#Sheet 1 - source sheet page having testnumber and worksheet number
my $source_sheet = $source_book->{Worksheet}[0];            #It is used to access worksheet

$source_cell = $source_sheet->{Cells}[1][0];                #Reads content of the cell;
my $seleniumHost = $source_cell->Value; 
print $seleniumHost,"\n";

open (F, '+>>',"$destname") or die "Couldn't open `$destname': $!";
my $line;

while ($line = <F>){
print $line;
if($line=~m/FTP/){
#next if /FTP/;
print $line;
print F $seleniumHost;}

Answers


The perlfaq covers this. How do I change, delete, or insert a line in a file, or append to the beginning of a file?

Files are fixed blocks of data. They behave much like a piece of paper. How do you insert a line into the middle of a piece of paper? You can't, not unless you left space. You must recopy the whole thing, inserting your line into the new copy.


Need Your Help

PE Header for Each NetModule in Assembly

.net assemblies clr

As we know, we can store netmodules in assemblies instead of putting them into different files and linked them in Assembly. But as each Managed Modules have their own PE Header, CLR Header, Metadat...

C: float changing to 'nan' value

c

I have an inner function in a larger program that is somehow changing a float value to "nan" when I expect it to be zero. I have trimmed the function down to the simplest form, with no parameters:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.