[英]Perl - a way to run a script multiple time and append output files
我想多次運行perl腳本,並將輸出文件附加到文件中。
假設我有一個perl腳本(sparc_sub.pl),該腳本生成一個輸出文件sparc_sub.output。 我想多次運行。 在每次運行時,它都會重新調整相同的輸出文件(sparc_sub.output),但是內容不同。
如果我想附加到文件末尾,我知道'>>'是符號,因此我使用該符號以循環方式打開輸出文件。 但是,輸出文件sparc_sub.output始終存儲上一次運行的結果。 (不附加)
任何幫助表示贊賞。
sparc_top.pl
use strict;
use Math::BigInt;
use 5.010;
use List::Util qw(sum);
my $s;
my $done=3;
my $script="sparc_sub.pl";
my @output;
my $filename ='sparc_sub.output';
for ($s=0; $s < $done; $s++) {
system('perl',$script)==0 or die "failed to execute $script: \$?=$?";
open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
close $fh;
}
您已經打開文件,然后立即關閉它,甚至沒有寫入任何文件。
open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
print $fh "this data goes to file";
close $fh;
我有一個Perl腳本
sparc_sub.pl
,它會生成一個輸出文件sparc_sub.output
因此,每次運行都會生成相同的文件。 我認為您想創建一個新文件,該文件將不斷追加sparc_sub.output
的數據。 如果是這種情況,請執行以下操作。
open(my $fh2, '>>', 'all_outputs') or die "Could not open file 'all_outputs' $!";
for ($s=0; $s < $done; $s++) {
system('perl',$script)==0 or die "failed to execute $script: \$?=$?";
#sparc_sub.output has been generated.
#Now take the data from this file and append it to a final file
open(my $fh, '<', 'sparc_sub.output') or die "Could not open file '$filename' $!";
my @data = <$fh>;
print $fh2 @data;
}
現在, all_outputs
文件包含來自sparc_sub.output
文件的數據,該數據在循環中多次生成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.