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

Perl 如何在现有文件的中间插入一行,perl,Perl,考虑一个示例,在该示例中,当 特定模式匹配(如果$line=~m/这里只有几行/,则 在下一行中插入行): 这是我的代码: 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->Pars

考虑一个示例,在该示例中,当 特定模式匹配(如果$line=~m/这里只有几行/,则 在下一行中插入行):

这是我的代码:

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;}
my$sourcename=$ARGV[1];
我的$destname=$ARGV[0];
打印$sourcename,“\n”;
打印$destname,“\n”;
my$source\u excel=新电子表格::ParseExcel;
我的$source\u book=$source\u excel->Parse($sourcename)或die“无法打开源代码excel文件$sourcename:$!”;
我的$source\u单元;
#表1-具有测试编号和工作表编号的源表页
my$source_sheet=$source_book->{sheet}[0]#它用于访问工作表
$source_cell=$source_sheet->{Cells}[1][0]#读取单元格的内容;
my$seleniumHost=$source\u cell->Value;
打印$seleniumHost,“\n”;
打开(F,“+>>”,“$destname”)或死亡“无法打开“$destname”:$!”;
我的美元线;
而($line=){
打印$行;
如果($line=~m/FTP/){
#下一个if/FTP/;
打印$行;
打印F$seleniumHost;}
在perl一行程序中:

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
如果你不想要一行,那么在任何脚本中都可以很容易地进行替换;)

perlfaq涵盖了这一点


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

有人提到了Tie::File,这是编辑文件时我必须考虑的解决方案,但是我通常使用File::Slurp,它最近添加了edit_File和edit_File_line subs。

使用perl的就地编辑标志(-I),使用perl向现有文件添加行很容易,只要您可以键入文本字符串,例如(在您的情况下)“想在这里插入几行”:

它会用一个旧句子的副本(没有丢失)加上您想要注入的新内容覆盖您的旧句子(不要害怕)。如果您愿意,您甚至可以通过将“.backup”扩展名传递到-i标志来创建原始文件的备份:

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
有关Perl搜索和替换功能的更多信息,请参见:


可以避免使用变量替换重复“标记”文本

echo-e“第一行\n第三行”| perl-pe的/(^first-line$)/\1\n第二行/'

只要您知道该行:

perl -ne 'if ($. == 8) {s//THIS IS NEW!!!\n/}; print;' 
显然,您必须使用-i来进行实际更改 或:


Thanx用于重播,但我不想替换文本,我想在下一行插入文本行而不覆盖任何文本。可能吗?我想在文件的中间插入(我不想过度写入)但是perlfaq建议创建另一个文件并在那里写入。@Sirga:即使是您现有的代码也会给人一种想要有不同的源文件和目标文件的印象。但是,如果您想要一种简单的方法,请看一下Tie::file。还有其他可能更有效的方法,但很难打败Tie的简单性:文件。@DavidO:我想做以下事情。1)从excel文件读取数据。2)打开一个文本文件,搜索匹配的模式,然后写入数据(从excel读取)进入下一行而不重写。@sirga在某个时候,您必须从插入点向前重写文件。请参阅上面的纸上类比。这就是文件的工作方式。您可能需要某种类型的数据库来处理搜索和插入。SQLite甚至BerkeleyDB都很简单。也许您可以告诉我们更多关于您正在做的事情你想完成什么?这个文件有多大,里面有什么?你能控制格式吗?有很多方法可以完成显示的内容,但你到底需要什么?你如何确定插入文本的位置?它是否需要按显示的格式?也许你的要求应该更清楚。谢谢。但是我们能做到专业吗使用File::Slurp处理每个错误?
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
perl -ne 'if ($. == 8) {s//THIS IS NEW!!!\n/}; print;' 
perl -i -pe 'if($. == 8) {s//THIS IS NEW!!!\n/}' file