我想添加一行在Perl這在排序的形式有數據的文本文件。我已經看到了一些示例,說明如何在文件的末尾附加數據,但是因爲我希望數據採用有序格式。的Perl:如何添加一行來排序的文本文件
請指導我如何可以做到的。
基本上從我到目前爲止已經嘗試的: (我打開一個文件,grep它的內容,看看我要添加到文件中的行是否已經存在,如果沒有,那麼將其添加到文件中(使得數據保持在一個排序格式)
open(my $FH, $file) or die "Failed to open file $file \n";
@file_data = <$FH>;
close($FH);
my $line = grep (/$string1/, @file_data);
if($line) {
print "Found\n";
exit(1);
}
else
{
#add the line to the file
print "Not found!\n";
}
用於就地在Perl PLS編輯看[此] [1] [1]:http://stackoverflow.com/questions/12070115/reading-and-writing-to-an-existing -file-in-perl – Vijay