如何读取大文件并写入perl中的文本文件

时间:2014-01-29 10:32:54

标签: perl ssh

我想从运行在unix中的远程主机中的文件中捕获数据,并使用perl通过文件句柄将其打印到文本文件。实际上我有一个项目列表,并尝试通过shell命令为每个项目创建文本文件.text文件已创建,但没有内容。

**数据可能很大。

foreach my $proj(@proj_list)
{
        my $tmpl; #template file path
        open(my $fh, '>', $filename) or die "Could not open file $filename $!";
        my $cmd="findbug -p $proj |dumpbug -fnt $tmpl";
        my $line1=$ssh->capture($cmd);
        print $fh "$line1";
        close($fh); 
}

从远程主机读取后,如何通过shell命令将大数据打印到文本文件中。

1 个答案:

答案 0 :(得分:-2)

除非有理由不能这样做,否则只需使用scp从远程服务器复制文件。

或者,我们使用了几个SSH模块,并恢复使用IPC :: Run3来运行ssh作为子进程。 IPC :: Run3的一大优点是,您可以非常轻松地将stdout和stderr重定向到父进程的文件,变量或文件句柄。

use IPC::Run3;
use File::Slurp qw(write_file);

my ($in, $out, $err);
my $cmd = "ssh -l $user $host findbug -p $proj |dumpbug -fnt $tmpl";
run3( $cmd, $in, $out, $err);
write_file($tmp_file_name, $out);

应该这样做。

相关问题