考虑一个例子,我想在特定模式匹配时插入几行文本(如果$ line = ~m /这里有几行/然后在下一行插入行):
Run Code Online (Sandbox Code Playgroud)*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."
这是我的代码:
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;}
Run Code Online (Sandbox Code Playgroud)
perlfaq涵盖了这一点. 如何更改,删除或插入文件中的行,或附加到文件的开头?
文件是固定的数据块.它们的表现就像一张纸.你如何在一张纸的中间插入一条线?你不能,除非你留下空间.您必须重新复制整个内容,将您的行插入新副本.
| 归档时间: |
|
| 查看次数: |
13041 次 |
| 最近记录: |