Sirga
Sirga

Reputation: 246

How to insert a line into the middle of an 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;}

Upvotes: 5

Views: 17394

Answers (6)

Paul-Gerhard Woolcock
Paul-Gerhard Woolcock

Reputation: 31

As long as you know the line:

perl -ne 'if ($. == 8) {s//THIS IS NEW!!!\n/}; print;' 

Obviously you'd have to use -i to make the actual changes OR:

perl -i -pe 'if($. == 8) {s//THIS IS NEW!!!\n/}' file

Upvotes: 1

Alfredo Diaz
Alfredo Diaz

Reputation: 658

You can avoid having to repeat the "markup" text using variable substitution.

echo -e "first line\nthird line" | perl -pe 's/(^first line$)/\1\nsecond line/'

Upvotes: 0

Rob Bailey
Rob Bailey

Reputation: 1787

Using perl's in-place edit flag (-i), it's easy to add lines to an existing file using Perl, as long as you can key off a text string, such as (in your case) "wanna insert few lines in here":

perl -pi -e 's{wanna insert few lines in here}{wanna insert few lines in here  this is my\nnew text which i wanted to insert }' filename

It overwrites your old sentence (don't be scared) with a copy of your old sentence (nothing lost) plus the new stuff you want injected. You can even create a backup of the original file if you wish by passing a ".backup" extension to the -i flag:

perl -p -i'.backup' -e 's{wanna insert few lines in here}{wanna insert few lines in here  this is my\nnew text which i wanted to insert }' filename

More info on Perl's search & replace capabilities can be found here:

http://www.atrixnet.com/in-line-search-and-replace-in-files-with-real-perl-regular-expressions/

Upvotes: 0

cajoki
cajoki

Reputation: 43

Someone mentioned Tie::File, which is a solution I'll have to look at for editing a file, but I generally use File::Slurp, which has relatively recently added edit_file and edit_file_lines subs.

Upvotes: 0

Schwern
Schwern

Reputation: 164699

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.

Upvotes: 8

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185025

In a perl one-liner :

perl -ane 's/few lines in here  and other\n/this is my\nnew text which i wanted to insert and other /; s/continue./\ncontinue./; print ' FILE

If you don't want a one-liner, it's easy to takes the substitutions in any script ;)

Upvotes: 3

Related Questions