gpt4 book ai didi

perl - 如何在现有文件的中间插入一行

转载 作者:行者123 更新时间:2023-12-02 07:44:05 24 4
gpt4 key购买 nike

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."


这是我的代码:
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;}

最佳答案

perlfaq 涵盖了这一点。 How do I change, delete, or insert a line in a file, or append to the beginning of a file?

文件是固定的数据块。它们的行为很像一张纸。如何在一张纸的中间插入一条线?你不能,除非你留了空间。您必须重新复制整个内容,将您的行插入新副本中。

关于perl - 如何在现有文件的中间插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8321643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com