【发布时间】:2015-12-11 03:07:48
【问题描述】:
我必须访问一个 shell 命令 - Perl 脚本中的 hive,所以我使用 `...`。 假设 `hive ... ...` 的结果包含 100000000 行,大小为 20GB。 我想要达到的效果是这样的:
@array = `hive ... ...`;
`` 是否自动知道使用“\n”作为分隔符将每一行划分为@array?
我能想到的两种方法是(但在这种情况下有问题):
$temp = `hive ... ...`;
@array = split ( "\n", $temp );
undef $temp;
这种方式的问题是,这种情况下如果hive的输出太大,$temp无法存储输出,导致segmentation fault core dump。
或
`hive ... ... 1>temp.txt`;
open ( FP, <, "temp.txt" );
while (<FP>)
{
chomp;
push @array, $_;
}
close FP;
`rm temp.txt`;
但是这种方式太慢了,因为它首先将结果写入硬盘。
有没有办法在不使用任何“临时容器”的情况下将 shell 命令的输出直接写入数组?
非常感谢您的帮助。
【问题讨论】:
标签: arrays perl shell system stdout